Hash is an encryption algorithm to convert text into a string of characters random. The number of characters in the hash result is always the same. Hash is one-way encryption, making the result of the hash unable to be returned to the original text. SHA1 or Secure Hash Algorithm 1 is a hashing algorithm which is often used for data encryption. The result of sha1 is data with 20 bytes or 160 bits wide, usually displayed in number form
40 digit hexadecimal. In this chapter we will learn about the use of sha1 and salting techniques in hash.
Go provides the crypto/sha1 package, containing a library for data hashing needs, how to implement it and examples
package main
import "crypto/sha1"
import "fmt"
func main() {
var text = "this is secret"
var sha = sha1.New()
sha.Write([]byte(text))
var encrypted = sha.Sum(nil)
var encryptedString = fmt.Sprintf("%x", encrypted)
fmt.Println(encryptedString)
// f4ebfd7a42d9a43a536e2bed9ee4974abf8f8dc8
}
The result variable of sha1.New() is an object of type hash.Hash , having two the Write() and Sum() methods. The Write() method is used to set the data to be hashed. Data
must be in []byte form. The Sum() method is used to execute the hash process, producing data which has been hashed in the form of []byte . This method requires a parameters, fill with nil. To retrieve a hexadecimal string form of hashed data, you can
utilize the fmt.Sprintf function with the %x format layout
If you found this helpful and would like to give my project a star for support, I would greatly appreciate it!
Top comments (4)
In 2024 we should not be using sha1 due to its collision resistance. Avoid using SHA-1 for new applications.
what about base64
base64 is good, a good example is creating a checksum(digital signature) for a url. datagenx.net/2023/08/hashing-check...
Thanks For the advice