func init() { hash2Name[crypto.SHA256] = "sha256" hash2Name[crypto.SHA512] = "sha512" crypto.RegisterHash(crypto.SHA256, sha256.New) crypto.RegisterHash(crypto.SHA512, sha512.New) for h, n := range hash2Name { name2Hash[n] = h } }
func main() { m := md5.New() str := "test" m.Write([]byte(str)) fmt.Printf("%x\n", m.Sum(nil)) crypto.RegisterHash(crypto.MD5, md5.New) fmt.Println(crypto.MD5.Available()) cm := crypto.MD5.New() cm.Write([]byte(str)) //fmt.Printf("%x\n", cm.Sum(nil)) fmt.Printf("%x\n", cm.Sum([]byte("test"))) fmt.Printf("TestFlag0:%d,TestFlag1:%d,TestFlag2:%d,TestFlag3:%d\n", TestFlag0, TestFlag1, TestFlag2, TestFlag3) }
//RegisterHashes uses go's pluggable hash registry to add gocryptodev //hashes as the default hash implementation. func RegisterHashes() { digest, err := New(CRYPTO_MD5) if err != nil { crypto.RegisterHash(crypto.MD5, func() hash.Hash { return digest }) } digest, err = New(CRYPTO_SHA1) if err == nil { crypto.RegisterHash(crypto.SHA1, func() hash.Hash { return digest }) } digest, err = New(CRYPTO_SHA2_256) if err == nil { crypto.RegisterHash(crypto.SHA256, func() hash.Hash { return digest }) } digest, err = New(CRYPTO_SHA2_384) if err == nil { crypto.RegisterHash(crypto.SHA384, func() hash.Hash { return digest }) } digest, err = New(CRYPTO_SHA2_512) if err == nil { crypto.RegisterHash(crypto.SHA512, func() hash.Hash { return digest }) } digest, err = New(CRYPTO_RIPEMD160) if err == nil { crypto.RegisterHash(crypto.RIPEMD160, func() hash.Hash { return digest }) } }
func init() { crypto.RegisterHash(crypto.SHA1, New) }
func init() { crypto.RegisterHash(crypto.MD5, New) }
func init() { crypto.RegisterHash(crypto.RIPEMD160, New) }
func init() { crypto.RegisterHash(crypto.SHA384, New384) crypto.RegisterHash(crypto.SHA512, New) crypto.RegisterHash(crypto.SHA512_224, New512_224) crypto.RegisterHash(crypto.SHA512_256, New512_256) }
func init() { crypto.RegisterHash(crypto.SHA224, New224) crypto.RegisterHash(crypto.SHA256, New) }
func init() { crypto.RegisterHash(crypto.SHA384, New384) crypto.RegisterHash(crypto.SHA512, New) }
func init() { crypto.RegisterHash(crypto.MD5, New) // 将md5注册进crypto }
func init() { crypto.RegisterHash(crypto.SHA1, New) // 注册SHA1 HASH函数 }