Home >Backend Development >Golang >Golang asks for hash

Golang asks for hash

王林
王林Original
2023-05-16 11:21:07910browse

Hash is a function used to compress a message of any length into a fixed-length digest. In general, a hash function maps any input to a fixed-length string of outputs. Different inputs may map to the same output, which is called a Hash Collision. Hash functions are widely used in information security, data integrity checking and other fields. In this article, we will introduce how to use hash functions in Go language.

There are many types of hash functions, and each hash function has its own unique characteristics, such as the length of the hash value, security, speed, etc. In Go language, commonly used hash functions include MD5, SHA-1, SHA-256, etc. These functions implement the crypto.Hash interface and can be used to generate message digests.

Below we will introduce the use of MD5, SHA-1 and SHA-256 functions respectively.

MD5

MD5 is a widely used hash function that compresses messages of any length into a 128-bit hash value. In the Go language, you can use the New function of the crypto/md5 package to create the MD5 hash function. Calling this function will return a Hash type pointer. You can use this pointer to call the Write method to input the data for which the hash value needs to be calculated, and finally call the Sum method to obtain the calculated hash value. The sample code is as follows:

package main

import (
    "crypto/md5"
    "fmt"
)

func main() {
    data := []byte("hello world")
    hash := md5.New()
    hash.Write(data)
    fmt.Printf("%x
", hash.Sum(nil))
}

The output result is:

5eb63bbbe01eeed093cb22bb8f5acdc3

SHA-1

SHA-1 is a hash function based on the MD5 algorithm, which will The message is compressed into a 160-bit hash value. In the Go language, you can use the New function of the crypto/sha1 package to create a SHA-1 hash function. Calling this function will return a Hash type pointer. You can use this pointer to call the Write method to input the data for which the hash value needs to be calculated, and finally call the Sum method to obtain the calculated hash value. The sample code is as follows:

package main

import (
    "crypto/sha1"
    "fmt"
)

func main() {
    data := []byte("hello world")
    hash := sha1.New()
    hash.Write(data)
    fmt.Printf("%x
", hash.Sum(nil))
}

The output result is:

2ef7bde608ce5404e97d5f042f95f89f1c232871

SHA-256

SHA-256 is a hash function that compresses a message of any length into a 256-bit hash value. In the Go language, you can use the New function of the crypto/sha256 package to create a SHA-256 hash function. Calling this function will return a Hash type pointer. You can use this pointer to call the Write method to input the data for which the hash value needs to be calculated, and finally call the Sum method to obtain the calculated hash value. The sample code is as follows:

package main

import (
    "crypto/sha256"
    "fmt"
)

func main() {
    data := []byte("hello world")
    hash := sha256.New()
    hash.Write(data)
    fmt.Printf("%x
", hash.Sum(nil))
}

The output result is:

b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

As you can see, the hash values ​​generated by different hash functions have different lengths. The appropriate hash function should be selected according to specific needs in the application.

Summary: The hash function can compress a message of any length into a fixed-length hash value. The Go language provides a variety of hash function implementations, and we can choose the appropriate hash function according to actual needs. Hash functions are widely used in fields such as information security and data integrity checking.

The above is the detailed content of Golang asks for hash. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:golang has no classNext article:golang has no class