blob: 437dcb21ba076faf9dc9dcd234258425db551748 (
plain) (
blame)
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
|
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019-2022 Bernd Fix >Y<
//
// gnunet-go 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.
//
// gnunet-go 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 this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later
package crypto
import (
"bytes"
"crypto/sha512"
"encoding/hex"
"gnunet/util"
)
// HashCode is the result of a 512-bit hash function (SHA-512)
type HashCode struct {
Bits []byte `size:"64"`
}
// Equals tests if two hash results are equal.
func (hc *HashCode) Equals(n *HashCode) bool {
return bytes.Equal(hc.Bits, n.Bits)
}
// Clone the hash code
func (hc *HashCode) Clone() *HashCode {
return NewHashCode(hc.Bits)
}
// String returns a hex-representation of the hash code
func (hc *HashCode) String() string {
return hex.EncodeToString(hc.Bits)
}
// NewHashCode creates a new (initialized) hash value
func NewHashCode(buf []byte) *HashCode {
hc := &HashCode{
Bits: make([]byte, 64),
}
if buf != nil {
util.CopyAlignedBlock(hc.Bits, buf)
}
return hc
}
// Hash returns the SHA-512 hash value of a given blob
func Hash(data []byte) *HashCode {
val := sha512.Sum512(data)
return &HashCode{
Bits: util.Clone(val[:]),
}
}
|