/
hash.go
48 lines (39 loc) · 914 Bytes
/
hash.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
//main package has examples shown
// in Go Data Structures and algorithms book
package main
// importing fmt package
import (
"fmt"
// "strconv"
"crypto/sha1"
"hash"
)
//CreateHash method
func CreateHash(byteStr []byte) []byte {
var hashVal hash.Hash
hashVal = sha1.New()
hashVal.Write(byteStr)
var bytes []byte
bytes = hashVal.Sum(nil)
return bytes
}
// Create hash for Multiple Values method
func CreateHashMultiple(byteStr1 []byte, byteStr2 []byte) []byte {
return xor(CreateHash(byteStr1), CreateHash(byteStr2))
}
// XOR method
func xor(byteStr1 []byte, byteStr2 []byte) []byte {
var xorbytes []byte
xorbytes = make([]byte, len(byteStr1))
var i int
for i = 0; i < len(byteStr1); i++ {
xorbytes[i] = byteStr1[i] ^ byteStr2[i]
}
return xorbytes
}
// main method
func main() {
var bytes []byte
bytes = CreateHashMultiple([]byte("Check"), []byte("Hash"))
fmt.Printf("%x\n", bytes)
}