/
sha.go
47 lines (36 loc) · 1018 Bytes
/
sha.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
package sha
import (
"encoding/binary"
"github.com/OhYee/cryptography_and_network_security/util/blackhole"
"log"
)
var Logger = log.New(blockhole.BlackHole{}, "", 0)
type sha struct{}
func uint32InputInitial(input []byte) []byte {
rawLength := len(input) * 8
// Ensure l≡448 mod 512
input = append(input, 0x80)
for (len(input)*8)%512 != 448 {
input = append(input, 0x00)
}
// Add the length at the end of input
length := make([]byte, 8)
binary.BigEndian.PutUint64(length, uint64(rawLength))
input = append(input, length...)
return input
}
func uint64InputInitial(input []byte) []byte {
rawLength := len(input) * 8
// Ensure l≡448 mod 512
input = append(input, 0x80)
for (len(input)*8)%1024 != 896 {
input = append(input, 0x00)
}
// Add the length at the end of input
length := make([]byte, 8)
binary.BigEndian.PutUint64(length, uint64(0))
input = append(input, length...)
binary.BigEndian.PutUint64(length, uint64(rawLength))
input = append(input, length...)
return input
}