package subtle import ( "crypto/sha1" "crypto/sha256" "crypto/sha512" "errors" "hash" "xdx.jelly/xgcl/sm/sm3" ) // hashDigestSize maps hash algorithms to their digest size in bytes. var hashDigestSize = map[string]uint32{ "SHA1": uint32(20), "SHA256": uint32(32), "SHA384": uint32(48), "SHA512": uint32(64), "SM3": uint32(32), } var errNilHashFunc = errors.New("nil hash function") // GetHashDigestSize returns the digest size of the specified hash algorithm. func GetHashDigestSize(hash string) (uint32, error) { digestSize, ok := hashDigestSize[hash] if !ok { return 0, errors.New("invalid hash algorithm") } return digestSize, nil } // GetHashFunc returns the corresponding hash function of the given hash name. func GetHashFunc(hash string) func() hash.Hash { switch hash { case "SHA1": return sha1.New case "SHA256": return sha256.New case "SHA384": return sha512.New384 case "SHA512": return sha512.New case "SM3": return sm3.New default: return nil } }