-
Notifications
You must be signed in to change notification settings - Fork 26
/
sha1-hashes.go
48 lines (39 loc) · 1.39 KB
/
sha1-hashes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
// Go implements several hash functions in various crypto/* packages.
import (
"crypto/sha1"
"fmt"
)
/*
SHA1 hashes are frequently used to compute short identities for binary or text blobs.
For example, the git revision control system uses SHA1s extensively to identify
versioned files and directories.
Here’s how to compute SHA1 hashes in Go.
*/
// SHA1Hashes illustrate SHA1 hashing in go.
func SHA1Hashes() {
s := "sha1 this string"
// The pattern for generating a hash is sha1.New(),
// sha1.Write(bytes),
// then sha1.Sum([]byte{}).
// Here we start with a new hash.
h := sha1.New()
// Write expects bytes.
// If you have a string s, use []byte(s) to coerce it to bytes.
h.Write([]byte(s))
// This gets the finalized hash result as a byte slice.
// The argument to Sum can be used to append to an existing byte slice:
// it usually isn’t needed.
bs := h.Sum(nil)
// SHA1 values are often printed in hex,
// for example in git commits. Use the %x format verb to convert a hash results to a hex string.
fmt.Println(s)
fmt.Printf("%x\n", bs)
/*
You can compute other hashes using a similar pattern to the one shown above.
For example, to compute MD5 hashes import crypto/md5 and use md5.New().
Note that if you need cryptographically secure hashes,
you should carefully research hash strength!
https://en.wikipedia.org/wiki/Cryptographic_hash_function
*/
}