forked from btcsuite/btcd
/
hashfuncs.go
111 lines (90 loc) · 2.23 KB
/
hashfuncs.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2015 The Decred developers
// Copyright (c) 2016-2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package chainhash
import (
"crypto/sha256"
"github.com/phoreproject/go-x11/blake"
"github.com/phoreproject/go-x11/bmw"
"github.com/phoreproject/go-x11/groest"
"github.com/phoreproject/go-x11/jhash"
"github.com/phoreproject/go-x11/keccak"
"github.com/phoreproject/go-x11/skein"
)
// HashB calculates hash(b) and returns the resulting bytes.
func HashB(b []byte) []byte {
hash := sha256.Sum256(b)
return hash[:]
}
// HashH calculates hash(b) and returns the resulting bytes as a Hash.
func HashH(b []byte) Hash {
return Hash(sha256.Sum256(b))
}
// DoubleHashB calculates hash(hash(b)) and returns the resulting bytes.
func DoubleHashB(b []byte) []byte {
first := sha256.Sum256(b)
second := sha256.Sum256(first[:])
return second[:]
}
// DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a
// Hash.
func DoubleHashH(b []byte) Hash {
first := sha256.Sum256(b)
return Hash(sha256.Sum256(first[:]))
}
// QuarkHash calculates the quarkcoin hash of a specific value
func QuarkHash(data []byte) Hash {
bmw1 := bmw.New()
blake1 := blake.New()
groest1 := groest.New()
jhash1 := jhash.New()
keccak1 := keccak.New()
skein1 := skein.New()
var out1a [64]byte
var out2a [64]byte
out1 := out1a[:]
out2 := out2a[:]
blake1.Write(data)
blake1.Close(out1, 0, 0)
bmw1.Write(out1)
bmw1.Close(out2, 0, 0)
if out2[0]&8 != 0 {
groest1.Write(out2)
groest1.Close(out1, 0, 0)
} else {
skein1.Write(out2)
skein1.Close(out1, 0, 0)
}
groest1.Reset()
groest1.Write(out1)
groest1.Close(out2, 0, 0)
jhash1.Write(out2)
jhash1.Close(out1, 0, 0)
if out1[0]&8 != 0 {
blake1.Reset()
blake1.Write(out1)
blake1.Close(out2, 0, 0)
} else {
bmw1.Reset()
bmw1.Write(out1)
bmw1.Close(out2, 0, 0)
}
keccak1.Write(out2)
keccak1.Close(out1, 0, 0)
skein1.Reset()
skein1.Write(out1)
skein1.Close(out2, 0, 0)
if out2[0]&8 != 0 {
keccak1.Reset()
keccak1.Write(out2)
keccak1.Close(out1, 0, 0)
} else {
jhash1.Reset()
jhash1.Write(out2)
jhash1.Close(out1, 0, 0)
}
var out [32]byte
copy(out[:], out1)
return Hash(out)
}