This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
/
port_blake2b_c.go
88 lines (76 loc) · 1.52 KB
/
port_blake2b_c.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package argon2
import (
"encoding/binary"
"github.com/dchest/blake2b"
)
const (
blakeOutBytes = 64
)
func blakeLong(out []byte, in []byte) error {
var (
outlen = len(out)
outlenBytes = make([]byte, 4)
)
binary.LittleEndian.PutUint32(outlenBytes, uint32(outlen))
if len(out) <= blakeOutBytes {
hash, err := blake2b.New(&blake2b.Config{
Size: uint8(outlen),
})
if err != nil {
return err
}
if _, err = hash.Write(outlenBytes); err != nil {
return err
}
if _, err = hash.Write(in); err != nil {
return err
}
sum := hash.Sum(nil)
copy(out, sum)
return nil
}
var (
toProduce uint32
buffer [blakeOutBytes]byte
)
hash, err := blake2b.New(&blake2b.Config{
Size: blakeOutBytes,
})
if err != nil {
return err
}
if _, err = hash.Write(outlenBytes); err != nil {
return err
}
if _, err = hash.Write(in); err != nil {
return err
}
sum := hash.Sum(nil)
copy(out, sum[:blakeOutBytes/2])
out = out[blakeOutBytes/2:]
toProduce = uint32(outlen) - blakeOutBytes/2
for toProduce > blakeOutBytes {
copy(buffer[:], sum)
hash.Reset()
if _, err = hash.Write(buffer[:]); err != nil {
return err
}
sum = hash.Sum(nil)
copy(out, sum[:blakeOutBytes/2])
out = out[blakeOutBytes/2:]
toProduce -= blakeOutBytes / 2
}
copy(buffer[:], sum[:blakeOutBytes])
hash, err = blake2b.New(&blake2b.Config{
Size: uint8(toProduce),
})
if err != nil {
return err
}
if _, err = hash.Write(buffer[:]); err != nil {
return err
}
sum = hash.Sum(nil)
copy(out, sum)
return nil
}