-
Notifications
You must be signed in to change notification settings - Fork 2
/
datainput.go
256 lines (204 loc) · 6.13 KB
/
datainput.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2014 The Monero Developers. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"crypto/ecdsa"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"github.com/btcsuite/btcec"
)
// readLength is the length of a hash compression read in bytes.
const readLength = (65536 - 1)
// CmpPubKey compares two pubkeys and returns true if they are the same, else
// false. WARNING: Assumes the curves are equivalent!
func CmpPubKey(i, j *ecdsa.PublicKey) bool {
if i.X.Cmp(j.X) != 0 {
return false
}
if i.Y.Cmp(j.Y) != 0 {
return false
}
return true
}
// keyInKeyRing checks if a pubkey exists in the keyring.
func (kr *PublicKeyRing) keyInKeyRing(k *ecdsa.PublicKey) bool {
for _, key := range kr.Ring {
if CmpPubKey(k, &key) {
return true
}
}
return false
}
// ReadKeyRing reads a key ring of public keys from a file in JSON object format,
// and also inserts the pubkey of a keypair if it's not already present (handles
// bug in URS implementation).
func ReadKeyRing(filename string, kp *ecdsa.PrivateKey) (*PublicKeyRing, error) {
// Load the file
f, err := ioutil.ReadFile(filename)
if err != nil {
fError := errors.New("os.Open: Couldn't open keyring file.")
return nil, fError
}
// Unmarshall the loaded file into a map
var keyMap = make(map[string]string)
err = json.Unmarshal(f, &keyMap)
if err != nil {
str := fmt.Sprintf("json error: Couldn't unmarshall keyring file.", err)
jsonError := errors.New(str)
return nil, jsonError
}
kr := NewPublicKeyRing(uint(len(keyMap)))
// Stick the pubkeys into the keyring as long as it doesn't belong to the
// keypair given.
for i := 0; i < len(keyMap); i++ {
pkBytes, errDecode := hex.DecodeString(keyMap[strconv.Itoa(i)])
if errDecode != nil {
decodeError := errors.New("decode error: Couldn't decode hex.")
return nil, decodeError
}
pubkey, errParse := btcec.ParsePubKey(pkBytes, btcec.S256())
if errParse != nil {
return nil, errParse
}
ecdsaPubkey := ecdsa.PublicKey{pubkey.Curve, pubkey.X, pubkey.Y}
if kp == nil || !CmpPubKey(&kp.PublicKey, &ecdsaPubkey) {
kr.Add(ecdsaPubkey)
} else {
kr.Add(kp.PublicKey)
}
}
// Stick the keypair in if it's missing.
if kp != nil && !kr.keyInKeyRing(&kp.PublicKey) {
kr.Add(kp.PublicKey)
}
return kr, nil
}
// ReadKeyPair reads an ECDSA keypair a file in JSON object format.
// Example JSON input:
// {
// "privkey": "..."
// }
// It also checks if a pubkey is in the keyring and, if not, appends it to the
// keyring.
func ReadKeyPair(filename string) (*ecdsa.PrivateKey, error) {
// Load the file
f, err := ioutil.ReadFile(filename)
if err != nil {
fError := errors.New("os.Open: Couldn't open keypair file.")
return nil, fError
}
// Unmarshall the loaded file into a map.
var keyMap = make(map[string]string)
var pubkey *ecdsa.PublicKey
var privkey *ecdsa.PrivateKey
err = json.Unmarshal(f, &keyMap)
if err != nil {
jsonError := errors.New("json error: Couldn't unmarshall keypair file.")
return nil, jsonError
}
privBytes, errDecode := hex.DecodeString(keyMap["privkey"])
if errDecode != nil {
decodeError := errors.New("decode error: Couldn't decode hex for privkey.")
return nil, decodeError
}
// PrivKeyFromBytes doesn't return an error, so this could possibly be ugly.
privkeyBtcec, _ := btcec.PrivKeyFromBytes(btcec.S256(), privBytes)
pubBytes, errDecode := hex.DecodeString(keyMap["pubkey"])
if errDecode != nil {
decodeError := errors.New("decode error: Couldn't decode hex for privkey.")
return nil, decodeError
}
pubkeyBtcec, errParse := btcec.ParsePubKey(pubBytes, btcec.S256())
if errParse != nil {
return nil, errParse
}
// Assign the things to return
pubkey = &ecdsa.PublicKey{pubkeyBtcec.Curve,
pubkeyBtcec.X,
pubkeyBtcec.Y}
privkey = &ecdsa.PrivateKey{*pubkey, privkeyBtcec.D}
return privkey, nil
}
// StripTextFile removes line breaks from a text file to ensure that signatures
// of text files function correctly cross platform.
func StripTextFile(b []byte) []byte {
input := bytes.NewBuffer(b)
var output []byte
rScanner := bufio.NewScanner(input)
rScanner.Split(bufio.ScanLines)
for rScanner.Scan() {
str := rScanner.Text()
bstr := []byte(str)
output = append(output, bstr...)
}
return output
}
// GetTextFileData retrieves the []byte encoding of a text file from a path.
func GetTextFileData(filename string) ([]byte, error) {
f, err := ioutil.ReadFile(filename)
if err != nil {
fError := errors.New("os.Open: Couldn't open text file message.")
return nil, fError
}
return StripTextFile(f), nil
}
// GetSigFileData retrieves the []byte encoding of a signature file from a path.
func GetSigFileData(filename string) ([]byte, error) {
f, err := ioutil.ReadFile(filename)
if err != nil {
fError := errors.New("os.Open: Couldn't open text file message.")
return nil, fError
}
return StripTextFile(f), nil
}
// GetBinaryFileData retrieves the []byte encoding of a binary file from a path. It
// hashes the data it obtains sequentially to compress the file.
func GetBinaryFileData(filename string) ([]byte, error) {
// Open the file
f, err := os.Open(filename)
if err != nil {
fError := errors.New("os.Open: Couldn't open binary file message.")
return nil, fError
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
// Open the reader
r := bufio.NewReader(f)
var hashArray []byte
var isEOF = false
current := 0
var tempBuffer []byte
// Slice up the file by bytes, hash big byte chunks, and store the sequential
// hashes.
for !isEOF {
b, err := r.ReadByte()
if err != nil { // err is only != nil if EOF is hit
isEOF = true
}
tempBuffer = append(tempBuffer, b)
current++
if current%readLength == 0 || isEOF == true {
// Hash the data chunk, copy it to a slice, and store it
hash := sha256.Sum256(tempBuffer)
var hashSlice []byte
for i := 0; i < sha256.Size; i++ {
hashSlice = append(hashSlice, hash[i])
}
hashArray = append(hashArray, hashSlice...)
current = 0 // Reset counter
}
}
return hashArray, nil
}