ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # 哈希算法 ## 概念 称谓: 单向散列函数, 哈希函数, 杂凑函数, 消息摘要函数 接收的输入: 原像 输出: 散列值, 哈希值, 指纹, 摘要 ## 单向散列函数特性 1. 将任意长度的数据转换成固定长度的数据 2. 很强的抗碰撞性 3. 不可逆 1. MD4/MD5 - 不安全 - 散列值长度: 128bit == 16byte 2. sha1 - 不安全 - 散列值长度: 160bit == 20byte 3. sha2 - 安全 - sha224 - 散列值长度: 224bit == 28byte - sha256 - 散列值长度: 256== 32byte - sha384 - 散列值长度: 384bit == 48byte - sha512 - 散列值长度: 512bit == 64byte ## go中使用单向散列函数 ~~~ // 第一种方式, 直接调用sum // 适用于数据量比较小的情况 func Sum(data []byte) [Size]byte // 第二种方式 // 1. 创建哈希接口对象 func New() hash.Hash type Hash interface { // 通过嵌入的匿名io.Writer接口的Write方法向hash中添加更多数据,永远不返回错误 io.Writer // 返回添加b到当前的hash值后的新切片,不会改变底层的hash状态 Sum(b []byte) []byte // 重设hash为无数据输入的状态 Reset() // 返回Sum会返回的切片的长度 Size() int // 返回hash底层的块大小;Write方法可以接受任何大小的数据, // 但提供的数据是块大小的倍数时效率更高 BlockSize() int } type Writer interface { Write(p []byte) (n int, err error) } // 2. 往创建出的哈希对象中添加数据 hash.Hash.Write([]byte("添加的数据...")) hash.Hash.Write([]byte("添加的数据...")) hash.Hash.Write([]byte("添加的数据...")) hash.Hash.Write([]byte("添加的数据...")) // 3. 计算结果, md5就是散列值 md5 := hash.Sum(nil); // 散列值一般是一个二进制的字符串, 有些字符不可见, 需要格式化 // 格式化为16进制的数字串 - 0-9, a-f func EncodeToString(src []byte) string // 数据转换完成之后, 长度是原来的2倍 ~~~ # sha256 ~~~ //验证 myHash() // 使用sha256 func myHash() { // sha256.Sum256([]byte("hello, go")) // 1. 创建哈希接口对象 myHash := sha256.New() // 2. 添加数据 src := []byte("123 123...") myHash.Write(src) myHash.Write(src) myHash.Write(src) // 3. 计算结果 res := myHash.Sum(nil) // 4. 格式化为16进制形式 myStr := hex.EncodeToString(res) fmt.Printf("%s\n", myStr) } ~~~