Go language implements various hash algorithms
1. Implementation of various hash algorithms
package main import ( "crypto" "crypto/md5" "crypto/sha1" "crypto/sha256" "crypto/sha512" "encoding/hex" "fmt" "hash" "/x/crypto/md4" "/x/crypto/ripemd160" ) func main() { str1 := HashByType("Hello World", "md4", false) (str1) str2 := HashByCrypto("Hello World", crypto.MD4, false) (str2) } // Hash according to different hash types: md4, md5, sha1, ripemd160, sha256, sha512func HashByType(text string, hashType string, isHex bool) string { var hashInstance // Define hash instance switch hashType { // Select the hash type case "md4": hashInstance = () // "/x/crypto/md4" case "md5": hashInstance = () case "sha1": hashInstance = () case "ripemd160": hashInstance = () // "/x/crypto/ripemd160" case "sha256": hashInstance = () case "sha512": hashInstance = () } if isHex { arr, _ := (text) // Convert hexadecimal string to hexadecimal byte array (arr) // Write to hash instance object } else { ([]byte(text)) // Convert string to byte array and write to hash object } bytes := (nil) // Append the hash value to the parameter, only get the original value, no need to append it, use nil, return the hash value byte array return ("%x", bytes) // Format output hash value} // Hash according to different hash types: md4, md5, sha1, ripemd160, sha256, sha512func HashByCrypto(text string, myhash , isHex bool) string { hashInstance := () // Define hash instance if isHex { arr, _ := (text) // Convert hexadecimal string to hexadecimal byte array (arr) // Write to hash instance object } else { ([]byte(text)) // Convert string to byte array and write to hash object } bytes := (nil) // Append the hash value to the parameter, only get the original value, no need to append it, use nil, return the hash value byte array return ("%x", bytes) // Format output hash value}
The supported Hash algorithms are:
const ( MD4 Hash = 1 + iota // import /x/crypto/md4 MD5 // import crypto/md5 SHA1 // import crypto/sha1 SHA224 // import crypto/sha256 SHA256 // import crypto/sha256 SHA384 // import crypto/sha512 SHA512 // import crypto/sha512 MD5SHA1 // no implementation; MD5+SHA1 used for TLS RSA RIPEMD160 // import /x/crypto/ripemd160 SHA3_224 // import /x/crypto/sha3 SHA3_256 // import /x/crypto/sha3 SHA3_384 // import /x/crypto/sha3 SHA3_512 // import /x/crypto/sha3 SHA512_224 // import crypto/sha512 SHA512_256 // import crypto/sha512 BLAKE2s_256 // import /x/crypto/blake2s BLAKE2b_256 // import /x/crypto/blake2b BLAKE2b_384 // import /x/crypto/blake2b BLAKE2b_512 // import /x/crypto/blake2b maxHash )
2. Two hash
- When there is a two consecutive hash requirement, in order not to repeatedly create hash objects and cause memory waste, the function can be encapsulated separately.
- After the hash is completed once, use the following statement to clear the hash object content and then perform a second hash.
() // Reset the hash instance
Complete code:
package main import ( "crypto" "crypto/md5" "crypto/sha1" "crypto/sha256" "crypto/sha512" "encoding/hex" "fmt" "hash" "/x/crypto/md4" "/x/crypto/ripemd160" ) func main() { str1 := HashByType("Hello World", "md4", false) (str1) str2 := HashByCrypto("Hello World", crypto.MD4, false) (str2) str3 := DoubleHashByCrypto("Hello World", crypto.MD4, false) (str3) } // Hash according to different hash types: md4, md5, sha1, ripemd160, sha256, sha512func HashByType(text string, hashType string, isHex bool) string { var hashInstance // Define hash instance switch hashType { // Select the hash type case "md4": hashInstance = () // "/x/crypto/md4" case "md5": hashInstance = () case "sha1": hashInstance = () case "ripemd160": hashInstance = () // "/x/crypto/ripemd160" case "sha256": hashInstance = () case "sha512": hashInstance = () } if isHex { arr, _ := (text) // Convert hexadecimal string to hexadecimal byte array (arr) // Write to hash instance object } else { ([]byte(text)) // Convert string to byte array and write to hash object } bytes := (nil) // Append the hash value to the parameter, only get the original value, no need to append it, use nil, return the hash value byte array return ("%x", bytes) // Format output hash value} // Hash according to different hash types: md4, md5, sha1, ripemd160, sha256, sha512func HashByCrypto(text string, myhash , isHex bool) string { hashInstance := () // Define hash instance if isHex { arr, _ := (text) // Convert hexadecimal string to hexadecimal byte array (arr) // Write to hash instance object } else { ([]byte(text)) // Convert string to byte array and write to hash object } bytes := (nil) // Append the hash value to the parameter, only get the original value, no need to append it, use nil, return the hash value byte array return ("%x", bytes) // Format output hash value} // Hash according to different hash types: md4, md5, sha1, ripemd160, sha256, sha512// The byte array after hashing 256 twice, the second time is to hash the hexadecimal after the first hashfunc DoubleHashByCrypto(text string, myhash , isHex bool) string { hashInstance := () // Define hash instance if isHex { arr, _ := (text) // Convert hexadecimal string to hexadecimal byte array (arr) // Write to hash instance object } else { ([]byte(text)) // Convert string to byte array and write to hash object } bytes := (nil) // Append the hash value to the parameter, only get the original value, no need to append it, use nil, return the hash value byte array () (bytes) bytes = (nil) return ("%x", bytes) // Format output hash value}
Output
77a781b995cf1cfaf39d9e2f5910c2cf
77a781b995cf1cfaf39d9e2f5910c2cf
487b5e8e42dcd5b4cc218c2e275561fb
This is the end of this article about implementing common hash algorithms using Go language. For more related Go hash algorithm content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!