Repository navigation
Expand file tree
/
Copy pathKeyUtils.java
More file actions
190 lines (142 loc) · 5.75 KB
/
Copy pathKeyUtils.java
File metadata and controls
190 lines (142 loc) · 5.75 KB
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
package controller;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.ECKey.ECDSASignature;
import org.bitcoinj.core.Sha256Hash;
import java.io.*;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.SecureRandom;
public class KeyUtils {
final private static char[] hexArray = "0123456789abcdef".toCharArray();
final private static String PRIV_KEY_FILENAME = "bitpay_private.key";
private static URI privateKey;
public KeyUtils() {
}
public static boolean privateKeyExists() {
return new File(PRIV_KEY_FILENAME).exists();
}
public static ECKey createEcKey() {
//Default constructor uses SecureRandom numbers.
return new ECKey();
}
public static ECKey createEcKeyFromHexString(String privateKey) {
//if you are going to choose this option, please ensure this string is as random as
//possible, consider http://world.std.com/~reinhold/diceware.html
SecureRandom randomSeed = new SecureRandom(privateKey.getBytes());
return new ECKey(randomSeed);
}
/**
* Convenience method.
*/
public static ECKey createEcKeyFromHexStringFile(String privKeyFile) throws IOException {
return createEcKeyFromHexString(getKeyStringFromFile(privKeyFile));
}
public static ECKey loadEcKey() throws IOException {
FileInputStream fileInputStream;
File file;
if (KeyUtils.privateKey == null) {
file = new File(PRIV_KEY_FILENAME);
} else {
file = new File(KeyUtils.privateKey);
}
byte[] bytes = new byte[(int) file.length()];
fileInputStream = new FileInputStream(file);
int numBytesRead = fileInputStream.read(bytes);
fileInputStream.close();
if (numBytesRead == -1) {
throw new IOException("read nothing from the file.");
}
return ECKey.fromASN1(bytes);
}
public static ECKey loadEcKey(URI privateKey) throws IOException, URISyntaxException {
KeyUtils.privateKey = privateKey;
File file = new File(privateKey);
if (!file.exists()) {
ECKey key = createEcKey();
saveEcKey(key, KeyUtils.privateKey);
return key;
}
return loadEcKey();
}
public static String getKeyStringFromFile(String filename) throws IOException {
BufferedReader br;
br = new BufferedReader(new FileReader(filename));
String line = br.readLine();
br.close();
return line;
}
public static void saveEcKey(ECKey ecKey) throws IOException {
byte[] bytes = ecKey.toASN1();
File file;
if (KeyUtils.privateKey == null) {
file = new File(PRIV_KEY_FILENAME);
} else {
file = new File(KeyUtils.privateKey);
}
FileOutputStream output = new FileOutputStream(file);
output.write(bytes);
output.close();
}
public static void saveEcKey(ECKey ecKey, URI privateKey) throws IOException, URISyntaxException {
File file = new File(privateKey);
//we shan't overwrite an existing file
if (file.exists()) {
return;
}
KeyUtils.privateKey = privateKey;
saveEcKey(ecKey);
}
public static String deriveSIN(ECKey ecKey) throws IllegalArgumentException {
// Get sha256 hash and then the RIPEMD-160 hash of the public key (this call gets the result in one step).
byte[] pubKeyHash = ecKey.getPubKeyHash();
// Convert binary pubKeyHash, SINtype and version to Hex
String version = "0F";
String SINtype = "02";
String pubKeyHashHex = bytesToHex(pubKeyHash);
// Concatenate all three elements
String preSIN = version + SINtype + pubKeyHashHex;
// Convert the hex string back to binary and double sha256 hash it leaving in binary both times
byte[] preSINbyte = hexToBytes(preSIN);
byte[] hash2Bytes = Sha256Hash.hashTwice(preSINbyte);
// Convert back to hex and take first four bytes
String hashString = bytesToHex(hash2Bytes);
String first4Bytes = hashString.substring(0, 8);
// Append first four bytes to fully appended SIN string
String unencoded = preSIN + first4Bytes;
byte[] unencodedBytes = new BigInteger(unencoded, 16).toByteArray();
return Base58.encode(unencodedBytes);
}
public static String sign(ECKey key, String input) throws UnsupportedEncodingException {
byte[] data = input.getBytes("UTF8");
Sha256Hash hash = Sha256Hash.of(data);
ECDSASignature sig = key.sign(hash, null);
byte[] bytes = sig.encodeToDER();
return bytesToHex(bytes);
}
private static int getHexVal(char hex) {
int val = (int) hex;
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
public static byte[] hexToBytes(String hex) throws IllegalArgumentException {
char[] hexArray = hex.toCharArray();
if (hex.length() % 2 == 1) {
throw new IllegalArgumentException("Error: The binary key cannot have an odd number of digits");
}
byte[] arr = new byte[hex.length() >> 1];
for (int i = 0; i < hex.length() >> 1; ++i) {
arr[i] = (byte) ((getHexVal(hexArray[i << 1]) << 4) + (getHexVal(hexArray[(i << 1) + 1])));
}
return arr;
}
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}