-
Notifications
You must be signed in to change notification settings - Fork 1
/
sha1_efes.go
64 lines (58 loc) · 1.87 KB
/
sha1_efes.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
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
)
// type digest struct {
// h [5]uint32
// x [chunk]byte
// nx int
// len uint64
// }
const (
sha1digestSize = 100
MaxUint = ^uint(0)
MaxInt = int(MaxUint >> 1)
)
var errInvalidDigest = errors.New("invalid digest")
func (d *sha1digest) MarshalText() ([]byte, error) { // nolint: unparam
b := bytes.NewBuffer(make([]byte, 0, sha1digestSize))
binary.Write(b, binary.BigEndian, d.h[0]) // nolint: errcheck
binary.Write(b, binary.BigEndian, d.h[1]) // nolint: errcheck
binary.Write(b, binary.BigEndian, d.h[2]) // nolint: errcheck
binary.Write(b, binary.BigEndian, d.h[3]) // nolint: errcheck
binary.Write(b, binary.BigEndian, d.h[4]) // nolint: errcheck
b.Write(d.x[:])
binary.Write(b, binary.BigEndian, int64(d.nx)) // nolint: errcheck
binary.Write(b, binary.BigEndian, d.len) // nolint: errcheck
ret := make([]byte, hex.EncodedLen(sha1digestSize))
hex.Encode(ret, b.Bytes())
return ret, nil
}
func (d *sha1digest) UnmarshalText(text []byte) error {
if len(text) != hex.EncodedLen(sha1digestSize) {
return errInvalidDigest
}
b := make([]byte, sha1digestSize)
_, err := hex.Decode(b, text)
if err != nil {
return errInvalidDigest
}
r := bytes.NewReader(b)
binary.Read(r, binary.BigEndian, &d.h[0]) // nolint: errcheck
binary.Read(r, binary.BigEndian, &d.h[1]) // nolint: errcheck
binary.Read(r, binary.BigEndian, &d.h[2]) // nolint: errcheck
binary.Read(r, binary.BigEndian, &d.h[3]) // nolint: errcheck
binary.Read(r, binary.BigEndian, &d.h[4]) // nolint: errcheck
r.Read(d.x[:]) // nolint: errcheck
var nx int64
binary.Read(r, binary.BigEndian, &nx) // nolint: errcheck
if nx > int64(MaxInt) {
return errInvalidDigest
}
d.nx = int(nx)
binary.Read(r, binary.BigEndian, &d.len) // nolint: errcheck
return nil
}