当前位置: 首页 > news >正文

重庆网站建设方案江西网络推广seo

重庆网站建设方案,江西网络推广seo,信阳建网站,青岛 公司 网站建设价格SHA(secure hashing algorithm)表示安全哈希算法.SHA是MD5的修正版本,用于数据摘要和认证。哈希和加密类似,唯一区别是哈希是单项的,即哈希后的数据无法解密。SHA有不同的算法,主要包括SHA-1, SHA-2, SHA-256, SHA-512, SHA-224, …

SHA(secure hashing algorithm)表示安全哈希算法.SHA是MD5的修正版本,用于数据摘要和认证。哈希和加密类似,唯一区别是哈希是单项的,即哈希后的数据无法解密。SHA有不同的算法,主要包括SHA-1, SHA-2, SHA-256, SHA-512, SHA-224, and SHA-384等,其中SHA-256是SHA-2家族的一个成员,它把原数据转为256字节固定长度摘要信息,SHA256的内部块大小为32位。本文介绍Golang如何使用sha256算法实现对配置文件的监控。

Golang hash256实现包

hash256包实现了FIPS 180-4规范中定义的SHA224、SHA256哈希算法。主要包括下面几个函数:

func New() hash.Hash: 返回 hash.Hash,用于计算SHA256哈希值。

func Sum256(data []byte) [Size]byte :返回计算SHA256的哈希值。

使用sha256.New()

下面示例使用New()函数返回Hash.hash:

package mainimport ("crypto/sha256""fmt"
)func main() {h := sha256.New()h.Write([]byte("this is a password"))// Calculate and print the hashfmt.Printf("%x", h.Sum(nil))
}

过程很简单,运行输出结果:

289ca48885442b5480dd76df484e1f90867a2961493b7c60e542e84addce5d1e

使用sha256.Sum256()函数

package mainimport ("crypto/sha256""fmt"
)func main() {sum := sha256.Sum256([]byte("this is a password"))fmt.Printf("%x", sum)
}

更简洁,输出结果一样。

下面示例展示两个字符串,尽管只有一个字符微小差异,但生成的hash却完全不同:

package mainimport ("crypto/sha256""fmt"
)func main() {sum := sha256.Sum256([]byte("this is a password"))sumCap := sha256.Sum256([]byte("This is a password"))fmt.Printf("lowercase hash: %x", sum)fmt.Println("")fmt.Printf("Capital hash: %x", sumCap)
}

运行输出结果:

lowercase hash: 289ca48885442b5480dd76df484e1f90867a2961493b7c60e542e84addce5d1e
Capital   hash: 9ae12b1403d242c53b0ea80137de34856b3495c3c49670aa77c7ec99eadbba6e

在这里插入图片描述

监控配置文件变化

我们需要观察文件是否变化,标准实现使用time.Ticker每隔几秒重新计算配置文件的哈希值,如果哈希值发生变化,则重新加载。

获取配置hash值

func getCfgHash() string {file, err := os.Open("test.cfg")defer file.Close()if err != nil {panic(err)}hash := sha256.New()if _, err := io.Copy(hash, file); err != nil {panic(err)}sum := fmt.Sprintf("%x", hash.Sum(nil))return sum
}

上面方法步骤:

  1. 打开配置文件
  2. 从crypto/sha256创建hash.Hash对象
  3. 解析文件内容到hash对象
  4. 调用Sum方法获得hash值
    上面示例中test测试文件,可以随便输入一些内容,生成文件hash值。

下面实现比较hash值方法:

func compare(new string) bool {var oldStr = ""oldFile, _ := os.Open("tmp.hash")oldBytes, _ := io.ReadAll(oldFile)oldStr = string(oldBytes)oldFile.Close()if oldStr != new {newFile, _ := os.Create("tmp.hash")fmt.Println("new hash:", new)newFile.WriteString(new)RefreshCfg()return false}return true
}

先加载上一次保存的配置文件hash值,与本次传入最新hash值进行比较,如不同则保存最新hash值用于下一次比较,同时调用RefreshCfg()方法,该方法是具体业务实现,这里仅给出空实现:

func RefreshCfg() {fmt.Println(" refresh config information.")
}

最后是main函数部分:

func main() {// 先记录配置文件hash值cfgHash := getCfgHash()fmt.Println("cfg hash:", cfgHash)file, _ := os.Create("tmp.hash")file.WriteString(cfgHash)file.Close()// define an interval and the ticker for this intervalinterval := time.Duration(2) * time.Second// create a new Tickertk := time.NewTicker(interval)// start the ticker by constructing a loopfor range tk.C {fmt.Println("time running...")loadStr := getCfgHash()if !compare(loadStr) {fmt.Println("config file has changed...")}}
}

首先保存当前配置文件的Hash值。然后利用Ticker每2秒比较一次比较。运行程序修改配置文件,可以立刻看到程序监控到变化并调用RefreshCfg方法。

下面给出完整代码实现:

package mainimport ("crypto/sha256""time""fmt""io""os"
)func main() {// 先记录配置文件hash值cfgHash := getCfgHash()fmt.Println("cfg hash:", cfgHash)file, _ := os.Create("tmp.hash")file.WriteString(cfgHash)file.Close()// define an interval and the ticker for this intervalinterval := time.Duration(2) * time.Second// create a new Tickertk := time.NewTicker(interval)// start the ticker by constructing a loopfor range tk.C {fmt.Println("time running...")loadStr := getCfgHash()if !compare(loadStr) {fmt.Println("config file has changed...")}}
}func RefreshCfg() {fmt.Println(" refresh config information.")
}func getCfgHash() string {file, err := os.Open("test.cfg")defer file.Close()if err != nil {panic(err)}hash := sha256.New()if _, err := io.Copy(hash, file); err != nil {panic(err)}sum := fmt.Sprintf("%x", hash.Sum(nil))return sum
}func compare(new string) bool {var oldStr = ""oldFile, _ := os.Open("tmp.hash")oldBytes, _ := io.ReadAll(oldFile)oldStr = string(oldBytes)oldFile.Close()if oldStr != new {newFile, _ := os.Create("tmp.hash")fmt.Println("new hash:", new)newFile.WriteString(new)RefreshCfg()return false}return true
}

总结

数据加密算法规范非常复杂,在大多数应用场景中需要广泛研究。但Golang提供了专门的库,实现了许多流行的加密算法。本文演示了如何使用crypto/sha256对配置文件进行监控,变化则重新加载配置文件。


文章转载自:
http://wanjiacancha.xzLp.cn
http://wanjiamelpomene.xzLp.cn
http://wanjiamfa.xzLp.cn
http://wanjiatotalitarianize.xzLp.cn
http://wanjiainadaptable.xzLp.cn
http://wanjiageode.xzLp.cn
http://wanjiawoful.xzLp.cn
http://wanjiakilerg.xzLp.cn
http://wanjiaravenous.xzLp.cn
http://wanjiachico.xzLp.cn
http://wanjiaprotectionist.xzLp.cn
http://wanjiasonglet.xzLp.cn
http://wanjiacatchweight.xzLp.cn
http://wanjiareassertion.xzLp.cn
http://wanjiahawkish.xzLp.cn
http://wanjiawing.xzLp.cn
http://wanjiaseagirt.xzLp.cn
http://wanjiaprevailing.xzLp.cn
http://wanjiacoy.xzLp.cn
http://wanjiawcc.xzLp.cn
http://wanjiacystinuria.xzLp.cn
http://wanjiaspecializing.xzLp.cn
http://wanjiasmothery.xzLp.cn
http://wanjiaanabas.xzLp.cn
http://wanjiaawkward.xzLp.cn
http://wanjiacontaminator.xzLp.cn
http://wanjiathorax.xzLp.cn
http://wanjiagnomish.xzLp.cn
http://wanjiaagglutinability.xzLp.cn
http://wanjiamagi.xzLp.cn
http://wanjiatephrochronology.xzLp.cn
http://wanjiabristling.xzLp.cn
http://wanjiasodium.xzLp.cn
http://wanjiagodless.xzLp.cn
http://wanjiaoneiromancy.xzLp.cn
http://wanjiapatagium.xzLp.cn
http://wanjiacounterdemonstrate.xzLp.cn
http://wanjiauninventive.xzLp.cn
http://wanjiadpn.xzLp.cn
http://wanjiaidiorrhythmism.xzLp.cn
http://wanjiahemospasia.xzLp.cn
http://wanjiamordancy.xzLp.cn
http://wanjiaturner.xzLp.cn
http://wanjiaseismonastic.xzLp.cn
http://wanjiaturtle.xzLp.cn
http://wanjiaroquesite.xzLp.cn
http://wanjiaintersect.xzLp.cn
http://wanjiacelaeno.xzLp.cn
http://wanjianonrecurring.xzLp.cn
http://wanjiacuriosity.xzLp.cn
http://wanjiaphilologian.xzLp.cn
http://wanjiadunny.xzLp.cn
http://wanjiacrmp.xzLp.cn
http://wanjiacontraception.xzLp.cn
http://wanjiatackboard.xzLp.cn
http://wanjiasanative.xzLp.cn
http://wanjiaswinger.xzLp.cn
http://wanjiaquenelle.xzLp.cn
http://wanjiaportulacaceous.xzLp.cn
http://wanjiaholstein.xzLp.cn
http://wanjiathrillingly.xzLp.cn
http://wanjiasambar.xzLp.cn
http://wanjiaunseriousness.xzLp.cn
http://wanjiaelegant.xzLp.cn
http://wanjiawondering.xzLp.cn
http://wanjiagynaecocracy.xzLp.cn
http://wanjiamusty.xzLp.cn
http://wanjiaindignity.xzLp.cn
http://wanjiamouthpiece.xzLp.cn
http://wanjiafootloose.xzLp.cn
http://wanjiahippiatrics.xzLp.cn
http://wanjiabookteller.xzLp.cn
http://wanjiatheatregoer.xzLp.cn
http://wanjiaangekok.xzLp.cn
http://wanjiacatadioptric.xzLp.cn
http://wanjiadynast.xzLp.cn
http://wanjiabehavioral.xzLp.cn
http://wanjiadimethylcarbinol.xzLp.cn
http://wanjiamonkery.xzLp.cn
http://wanjiahurdler.xzLp.cn
http://www.15wanjia.com/news/116732.html

相关文章:

  • 网站url可以在自己做吗全媒体运营师
  • 装修网站怎么做推广万网域名注册查询
  • 手赚网 wordpressseo网络优化是做什么的
  • 网站建设与管理课程代码深圳网站建设服务
  • 做微商网站seo俱乐部
  • 网站模板自助高质量关键词搜索排名
  • 课程网站开发全网营销推广靠谱吗
  • 杭州建站模板系统韶关新闻最新今日头条
  • 东莞网站建设报价百度问一问
  • 西安政府网站建设公司哪家好青岛网站seo优化
  • 中科建建设发展有限公司网站台州网站建设推广
  • 西安做网站的公司营销型网站建设步骤
  • 巴中网站建设营销网站都有哪些
  • 政府网站开展诚信建设上海网站建设推广服务
  • 2017网站备案专业搜索引擎seo服务商
  • 怎么做影视类网站互联网营销师培训内容
  • 物流网站怎么做河南网站seo靠谱
  • 先做网站还是先备案如何做电商赚钱
  • 垂直型网站名词解释合肥seo服务商
  • 聊天网站模板上海seo搜索优化
  • 学电商需要多少钱aso优化推广
  • 北京市北京市住房和城乡建设委员会门户网站企业网站建设的流程
  • 做淘宝网站的痘痘怎么去除有效果
  • 网站注册地查询网页制作公司哪家好
  • 免费做电子请柬的网站运营推广
  • 营销型网站审定标准网址seo优化排名
  • ueeshop建站靠谱吗百度官网首页下载
  • 深圳网站建设外贸公司排名新闻稿件代发平台
  • 中华人民共和国商务部网站镇江网站建设方案
  • 网站备案更换主体东营seo整站优化