-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsha256.go
More file actions
48 lines (44 loc) · 971 Bytes
/
sha256.go
File metadata and controls
48 lines (44 loc) · 971 Bytes
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
import (
"bufio"
"crypto/sha256"
"crypto/sha512"
"errors"
"flag"
"fmt"
"log"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
c1 := sha256.Sum256([]byte("x"))
c2 := sha256.Sum256([]byte("X"))
encoding := flag.Int("hashlength", 256, "The size of the hash (default: 256)")
switch *encoding {
case 256:
hash := sha256.Sum256([]byte(text))
fmt.Printf("%x\n", hash)
case 384:
hash := sha512.Sum384([]byte(text))
fmt.Printf("%x\n", hash)
case 512:
hash := sha512.Sum512([]byte(text))
fmt.Printf("%x\n", hash)
default:
log.Fatal(errors.New("256, 384 and 512 are the only hash length options."))
}
d := diff(&c1, &c2)
fmt.Printf("%x\n", c1)
fmt.Printf("%x\n", c2)
fmt.Printf("There are %d different characters\n", d)
}
func diff(sha1 *[32]byte, sha2 *[32]byte) int {
var count int
for index, element := range sha1 {
if sha2[index] != element {
count++
}
}
return count
}