/
commit.go
81 lines (65 loc) · 1.55 KB
/
commit.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
/*
* Copyright (C) 2018-2019 Fusion Foundation Ltd. All rights reserved.
* Copyright (C) 2018-2019 changxing@fusion.org
*
* This library is free software; you can redistribute it and/or
* modify it under the Apache License, Version 2.0.
*
* This library 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 License for the specific language governing permissions and
* limitations under the License.
*
*/
package ed
import (
"bytes"
cryptorand "crypto/rand"
"crypto/sha256"
"crypto/sha512"
"fmt"
"io"
)
func Commit(secret [32]byte) ([32]byte, [64]byte) {
// Generate the random num
rand := cryptorand.Reader
var rndNum [32]byte
if _, err := io.ReadFull(rand, rndNum[:]); err != nil {
fmt.Println("Error: io.ReadFull(rand, rndNum[:])")
}
var D [64]byte
copy(D[:32], rndNum[:])
copy(D[32:], secret[:])
var rsDigest512 [64]byte
var C [32]byte
// hash by sha512
h := sha512.New()
h.Write(rndNum[:])
h.Write(secret[:])
h.Sum(rsDigest512[:0])
// hash by sha256
h = sha256.New()
h.Write(rsDigest512[:])
h.Sum(C[:0])
return C, D
}
func Verify(C [32]byte, D [64]byte) bool {
var rsDigest512 [64]byte
var rsDigest256 [32]byte
// hash by sha512
h := sha512.New()
h.Write(D[:32])
h.Write(D[32:])
h.Sum(rsDigest512[:0])
// hash by sha256
h = sha256.New()
h.Write(rsDigest512[:])
h.Sum(rsDigest256[:0])
if bytes.Equal(C[:], rsDigest256[:]) {
return true
} else {
return false
}
}