github.com/enetx/g@v1.0.80/int_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 Int for hashing. 13 type ihash struct{ int Int } 14 15 // Hash returns a ihash struct wrapping the given Int. 16 func (i Int) Hash() ihash { return ihash{i} } 17 18 // MD5 computes the MD5 hash of the wrapped Int and returns the hash as an String. 19 func (ih ihash) MD5() String { return intHasher(md5.New(), ih.int) } 20 21 // SHA1 computes the SHA1 hash of the wrapped Int and returns the hash as an String. 22 func (ih ihash) SHA1() String { return intHasher(sha1.New(), ih.int) } 23 24 // SHA256 computes the SHA256 hash of the wrapped Int and returns the hash as an String. 25 func (ih ihash) SHA256() String { return intHasher(sha256.New(), ih.int) } 26 27 // SHA512 computes the SHA512 hash of the wrapped Int and returns the hash as an String. 28 func (ih ihash) SHA512() String { return intHasher(sha512.New(), ih.int) } 29 30 // intHasher a helper function that computes the hash of the given Int using the specified hash.Hash algorithm and returns the hash as an String. 31 func intHasher(algorithm hash.Hash, ih Int) String { 32 _, _ = algorithm.Write(ih.Bytes()) 33 return String(hex.EncodeToString(algorithm.Sum(nil))) 34 }