Hash refers to a method of mapping a binary string of arbitrary length into a binary string of fixed length. The mapping rule is the hash algorithm, also known as the hash algorithm. Hash algorithms are often used in applications such as encryption, checking data integrity, and hash table lookup.
Go language (golang) provides the hash package in the standard library, which provides the implementation of a variety of hash algorithms, including the following:
This article will introduce in detail how to Use the hash package in golang to implement the hash algorithm.
MD5 (Message-Digest Algorithm 5, fifth edition of message digest algorithm) is a common hash algorithm, and its output result is 128 bits Binary string, usually represented by a 32-bit hexadecimal number. In golang, you can obtain an md5 hash object through the md5.New() method in the hash package, and use the Write method to write the data to be hashed into the hash object. Finally, the calculated hash value can be obtained through the Sum method.
The following is a sample code:
package main import ( "crypto/md5" "fmt" ) func main() { data := []byte("example data") hash := md5.Sum(data) fmt.Printf("%x", hash) }
The output result is:
7f808d6e58805c6bbfd1eb6b2f06991a
SHA1 (Secure Hash Algorithm 1, Secure Hash Algorithm (First Edition) is a relatively secure hash algorithm. Its output result is a 160-bit binary string, usually represented by a 40-digit hexadecimal number. In golang, you can obtain a sha1 hash object through the sha1.New() method in the hash package, and use the Write method to write the data to be hashed into the hash object. Finally, the calculated hash value can be obtained through the Sum method.
The following is a sample code:
package main import ( "crypto/sha1" "fmt" ) func main() { data := []byte("example data") hash := sha1.Sum(data) fmt.Printf("%x", hash) }
The output result is:
ee5a3dd464a36f28cd1f2ff8405348d7a48112d6
SHA256 (Secure Hash Algorithm 256, Secure Hash Algorithm Version 256) is a more secure hash algorithm. Its output result is a 256-bit binary string, usually represented by a 64-bit hexadecimal number. In golang, you can obtain a sha256 hash object through the sha256.New() method in the hash package, and use the Write method to write the data to be hashed into the hash object. Finally, the calculated hash value can be obtained through the Sum method.
The following is a sample code:
package main import ( "crypto/sha256" "fmt" ) func main() { data := []byte("example data") hash := sha256.Sum256(data) fmt.Printf("%x", hash) }
The output result is:
9b64dd6a22a2d1f2850835a089a58fbd572d8399f22b97d73a5e7dd2c2ae9307
SHA512 (Secure Hash Algorithm 512, Secure Hash Algorithm version 512) is one of the most secure hash algorithms. Its output result is a 512-bit binary string, usually represented by a 128-bit hexadecimal number. In golang, you can obtain a sha512 hash object through the sha512.New() method in the hash package, and use the Write method to write the data to be hashed into the hash object. Finally, the calculated hash value can be obtained through the Sum method.
The following is a sample code:
package main import ( "crypto/sha512" "fmt" ) func main() { data := []byte("example data") hash := sha512.Sum512(data) fmt.Printf("%x", hash) }
The output result is:
22a0c871d16be5f7dafa984087bfe0e0694fcc9a0daeff1f76fcd50445ce40ba7a45dbb13a3d0bba593bf813c368b4c14354faf7ea5718c2907f93bffb2c1253
CRC32 (Cyclic Redundancy Check 32, Cyclic redundancy check (32 bits) is a checksum algorithm used to verify data integrity. The output result is a 32-bit binary string, usually represented by an 8-digit hexadecimal number. In golang, you can obtain a crc32 hash object through the crc32.NewIEEE() method in the hash package, and use the Write method to write the data to be hashed into the hash object. Finally, the calculated hash value can be obtained through the Sum method.
The following is a sample code:
package main import ( "fmt" "hash/crc32" ) func main() { data := []byte("example data") hash := crc32.ChecksumIEEE(data) fmt.Printf("%x", hash) }
The output result is:
f1ade6eb
Adler32 is another verification The output result of the sum algorithm is a 32-bit binary string, usually represented by an 8-digit hexadecimal number. In golang, you can obtain an adler32 hash object through the adler32.New() method in the hash package, and use the Write method to write the data to be hashed into the hash object. Finally, the calculated hash value can be obtained through the Sum method.
The following is a sample code:
package main import ( "fmt" "hash/adler32" ) func main() { data := []byte("example data") hash := adler32.Checksum(data) fmt.Printf("%x", hash) }
The output result is:
142113ca
Blake2b is a newer Hash algorithm, the output result can be customized in length, the common ones are 256 bits, 384 bits and 512 bits. In golang, you can obtain a blate2b hash object of corresponding length through the blake2b.New256(), blake2b.New384() and blake2b.New512() methods in the hash package, and use the Write method to write the data to be hashed into the hash object. Finally, the calculated hash value can be obtained through the Sum method.
The following is a sample code:
package main import ( "fmt" "golang.org/x/crypto/blake2b" ) func main() { data := []byte("example data") hash256 := blake2b.Sum256(data) hash384 := blake2b.Sum384(data) hash512 := blake2b.Sum512(data) fmt.Printf("%x %x %x", hash256, hash384, hash512) }
The output result is:
d852969b47e35f938f9f0ca05f2d3ca9f965b085e6c35d4ec5dbd6d2df4475d9 75bdbe670d92c40a2e62c753bd241f3181f191a5c70fd715d8a5e341a45b607e0f0c342c1df2b4ecb103d1dee066ef2d 8f87a4e71bde7c58c9ecc1c2c8d7bb93805d378854a496b13a5bbdaf01c138bdd01fe265ff0550c7219362918f451422c8aa738f4a0517a8d4416b7c2653c403
SHA3 (Secure Hash Algorithm 3, Secure Hash Algorithm Version 3) is another newer hash algorithm whose output result length can be customized. The more common ones are 256 bits and 512 bits. In golang, you can obtain a sha3 hash object of corresponding length through the sha3.New256() and sha3.New512() methods in the hash package, and use the Write method to write the data to be hashed into the hash object. Finally, the calculated hash value can be obtained through the Sum method.
The following is a sample code:
package main import ( "fmt" "golang.org/x/crypto/sha3" ) func main() { data := []byte("example data") hash256 := sha3.Sum256(data) hash512 := sha3.Sum512(data) fmt.Printf("%x %x", hash256, hash512) }
The output result is:
992f61da74e7f165b6825ba7b92ea5e119f7f4da612a7fa3024bddda7c2b45f4 266dbb94654e7c4d3493f3f88f5aff2d13601aeff862a8195aaf2caa16e7b3cb35f0b91d6f3c44f9b1dfeca28da40282caf1a2e8d5ed18a6ebf858adcce4813d
The above is the detailed content of Golang implements hash. For more information, please follow other related articles on the PHP Chinese website!