github.com/enetx/g@v1.0.80/float_hash.go (about) 1 package g 2 3 import ( 4 "crypto/md5" 5 "crypto/sha1" 6 "crypto/sha256" 7 "crypto/sha512" 8 "encoding/hex" 9 "hash" 10 ) 11 12 // A struct that wraps an Float for hashing. 13 type fhash struct{ float Float } 14 15 // Hash returns a fhash struct wrapping the given Float. 16 func (f Float) Hash() fhash { return fhash{f} } 17 18 // MD5 computes the MD5 hash of the wrapped Float and returns the hash as an String. 19 func (fh fhash) MD5() String { return floatHasher(md5.New(), fh.float) } 20 21 // SHA1 computes the SHA1 hash of the wrapped Float and returns the hash as an String. 22 func (fh fhash) SHA1() String { return floatHasher(sha1.New(), fh.float) } 23 24 // SHA256 computes the SHA256 hash of the wrapped Float and returns the hash as an String. 25 func (fh fhash) SHA256() String { return floatHasher(sha256.New(), fh.float) } 26 27 // SHA512 computes the SHA512 hash of the wrapped Float and returns the hash as an String. 28 func (fh fhash) SHA512() String { return floatHasher(sha512.New(), fh.float) } 29 30 // floatHasher a helper function that computes the hash of the given Float using the specified 31 // hash.Hash algorithm and returns the hash as an String. 32 func floatHasher(algorithm hash.Hash, f Float) String { 33 _, _ = algorithm.Write(f.Bytes()) 34 return String(hex.EncodeToString(algorithm.Sum(nil))) 35 }