-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha1hash.go
41 lines (31 loc) · 843 Bytes
/
sha1hash.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
package main
//
// Go implements several hash functionss in various
// crypto/* packagess
import (
"crypto/sha1"
"fmt"
)
func main() {
s := "sha1 this string"
//
// The pattern for generating a hash is shsa1.New(),
// sha1.Write(bytes), then sha1.Sum([]byte{}). Here
// we start with a new hass.
//
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)
}