-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
93 lines (79 loc) · 2.75 KB
/
util.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
89
90
91
92
93
// Copyright (C) 2019 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package crypto
import (
"crypto/sha512"
"encoding/base32"
"encoding/binary"
"errors"
"fmt"
"hash"
)
// Hashable is an interface implemented by an object that can be represented
// with a sequence of bytes to be hashed or signed, together with a type ID
// to distinguish different types of objects.
type Hashable interface {
ToBeHashed() (HashID, []byte)
}
func HashRep(h Hashable) []byte {
hashid, data := h.ToBeHashed()
return append([]byte(hashid), data...)
}
// DigestSize is the number of bytes in the preferred hash Digest used here.
const DigestSize = sha512.Size256
// Digest represents a 32-byte value holding the 256-bit Hash digest.
type Digest [DigestSize]byte
// String returns the digest in a human-readable Base32 string
func (d Digest) String() string {
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(d[:])
}
// TrimUint64 returns the top 64 bits of the digest and converts to uint64
func (d Digest) TrimUint64() uint64 {
return binary.LittleEndian.Uint64(d[:8])
}
// IsZero return true if the digest contains only zeros, false otherwise
func (d Digest) IsZero() bool {
return d == Digest{}
}
// DigestFromString converts a string to a Digest
func DigestFromString(str string) (d Digest, err error) {
decoded, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(str)
if err != nil {
return d, err
}
if len(decoded) != len(d) {
msg := fmt.Sprintf(`Attempted to decode a string which was not a Digest: "%v"`, str)
return d, errors.New(msg)
}
copy(d[:], decoded[:])
return d, err
}
// Hash computes the SHASum512_256 hash of an array of bytes
func Hash(data []byte) Digest {
return sha512.Sum512_256(data)
}
// Hash computes the SHASum512_256 hash of an array of bytes
func TxHash(data []byte) Digest {
return Sha256(data)
}
// HashObj computes a hash of a Hashable object and its type
func HashObj(h Hashable) Digest {
return Hash(HashRep(h))
}
// NewHash returns a sha512-256 object to do the same operation as Hash()
func NewHash() hash.Hash {
return sha512.New512_256()
}