13
13
14
14
package io .nats .nkey ;
15
15
16
- import net .i2p .crypto .eddsa .EdDSAEngine ;
17
- import net .i2p .crypto .eddsa .EdDSAPrivateKey ;
18
- import net .i2p .crypto .eddsa .EdDSAPublicKey ;
19
- import net .i2p .crypto .eddsa .spec .EdDSAPrivateKeySpec ;
20
- import net .i2p .crypto .eddsa .spec .EdDSAPublicKeySpec ;
16
+ import static io .nats .nkey .NKeyConstants .ED25519_PRIVATE_KEYSIZE ;
17
+ import static io .nats .nkey .NKeyConstants .ED25519_PUBLIC_KEYSIZE ;
18
+ import static io .nats .nkey .NKeyConstants .ED25519_SEED_SIZE ;
19
+ import static io .nats .nkey .NKeyConstants .ED_25519 ;
20
+ import static io .nats .nkey .NKeyConstants .PREFIX_BYTE_ACCOUNT ;
21
+ import static io .nats .nkey .NKeyConstants .PREFIX_BYTE_CLUSTER ;
22
+ import static io .nats .nkey .NKeyConstants .PREFIX_BYTE_OPERATOR ;
23
+ import static io .nats .nkey .NKeyConstants .PREFIX_BYTE_SEED ;
24
+ import static io .nats .nkey .NKeyConstants .PREFIX_BYTE_SERVER ;
25
+ import static io .nats .nkey .NKeyConstants .PREFIX_BYTE_USER ;
26
+ import static io .nats .nkey .NKeyUtils .PRAND ;
27
+ import static io .nats .nkey .NKeyUtils .SRAND ;
28
+ import static io .nats .nkey .NKeyUtils .base32Decode ;
29
+ import static io .nats .nkey .NKeyUtils .base32Encode ;
30
+ import static io .nats .nkey .NKeyUtils .crc16 ;
21
31
22
32
import java .io .ByteArrayOutputStream ;
23
33
import java .io .IOException ;
24
34
import java .nio .ByteBuffer ;
25
35
import java .nio .ByteOrder ;
26
- import java .security .*;
36
+ import java .security .GeneralSecurityException ;
37
+ import java .security .KeyPair ;
38
+ import java .security .MessageDigest ;
39
+ import java .security .NoSuchAlgorithmException ;
40
+ import java .security .NoSuchProviderException ;
41
+ import java .security .PrivateKey ;
42
+ import java .security .PublicKey ;
43
+ import java .security .SecureRandom ;
44
+ import java .security .Signature ;
27
45
import java .util .Arrays ;
28
46
29
- import static io .nats .nkey .NKeyConstants .*;
30
- import static io .nats .nkey .NKeyUtils .*;
47
+ import net .i2p .crypto .eddsa .EdDSAEngine ;
48
+ import net .i2p .crypto .eddsa .EdDSAPrivateKey ;
49
+ import net .i2p .crypto .eddsa .EdDSAPublicKey ;
50
+ import net .i2p .crypto .eddsa .spec .EdDSAPrivateKeySpec ;
51
+ import net .i2p .crypto .eddsa .spec .EdDSAPublicKeySpec ;
31
52
32
53
public class NKey {
33
54
@@ -59,7 +80,7 @@ static char[] removePaddingAndClear(char[] withPad) {
59
80
return withoutPad ;
60
81
}
61
82
62
- static char [] encode (NkeyType type , byte [] src ) throws IOException {
83
+ static char [] encode (NKeyType type , byte [] src ) throws IOException {
63
84
ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
64
85
65
86
bytes .write (type .prefix );
@@ -74,7 +95,7 @@ static char[] encode(NkeyType type, byte[] src) throws IOException {
74
95
return removePaddingAndClear (withPad );
75
96
}
76
97
77
- static char [] encodeSeed (NkeyType type , byte [] src ) throws IOException {
98
+ static char [] encodeSeed (NKeyType type , byte [] src ) throws IOException {
78
99
if (src .length != ED25519_PRIVATE_KEYSIZE && src .length != ED25519_SEED_SIZE ) {
79
100
throw new IllegalArgumentException ("Source is not the correct size for an ED25519 seed" );
80
101
}
@@ -119,10 +140,10 @@ static byte[] decode(char[] src) {
119
140
return dataBytes ;
120
141
}
121
142
122
- static byte [] decode (NkeyType expectedType , char [] src ) {
143
+ static byte [] decode (NKeyType expectedType , char [] src ) {
123
144
byte [] raw = decode (src );
124
145
byte [] dataBytes = Arrays .copyOfRange (raw , 1 , raw .length );
125
- NkeyType type = NkeyType .fromPrefix (raw [0 ] & 0xFF );
146
+ NKeyType type = NKeyType .fromPrefix (raw [0 ] & 0xFF );
126
147
127
148
if (type != expectedType ) {
128
149
return null ;
@@ -150,7 +171,7 @@ static NKeyDecodedSeed decodeSeed(char[] seed) {
150
171
return new NKeyDecodedSeed (b2 , dataBytes );
151
172
}
152
173
153
- private static NKey createPair (NkeyType type , SecureRandom random )
174
+ private static NKey createPair (NKeyType type , SecureRandom random )
154
175
throws IOException , NoSuchProviderException , NoSuchAlgorithmException {
155
176
if (random == null ) {
156
177
random = SRAND ;
@@ -162,7 +183,7 @@ private static NKey createPair(NkeyType type, SecureRandom random)
162
183
return createPair (type , seed );
163
184
}
164
185
165
- private static NKey createPair (NkeyType type , byte [] seed )
186
+ private static NKey createPair (NKeyType type , byte [] seed )
166
187
throws IOException , NoSuchProviderException , NoSuchAlgorithmException {
167
188
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec (seed , ED_25519 );
168
189
EdDSAPrivateKey privKey = new EdDSAPrivateKey (privKeySpec );
@@ -190,7 +211,7 @@ private static NKey createPair(NkeyType type, byte[] seed)
190
211
*/
191
212
public static NKey createAccount (SecureRandom random )
192
213
throws IOException , NoSuchProviderException , NoSuchAlgorithmException {
193
- return createPair (NkeyType .ACCOUNT , random );
214
+ return createPair (NKeyType .ACCOUNT , random );
194
215
}
195
216
196
217
/**
@@ -205,7 +226,7 @@ public static NKey createAccount(SecureRandom random)
205
226
*/
206
227
public static NKey createCluster (SecureRandom random )
207
228
throws IOException , NoSuchProviderException , NoSuchAlgorithmException {
208
- return createPair (NkeyType .CLUSTER , random );
229
+ return createPair (NKeyType .CLUSTER , random );
209
230
}
210
231
211
232
/**
@@ -220,7 +241,7 @@ public static NKey createCluster(SecureRandom random)
220
241
*/
221
242
public static NKey createOperator (SecureRandom random )
222
243
throws IOException , NoSuchProviderException , NoSuchAlgorithmException {
223
- return createPair (NkeyType .OPERATOR , random );
244
+ return createPair (NKeyType .OPERATOR , random );
224
245
}
225
246
226
247
/**
@@ -235,7 +256,7 @@ public static NKey createOperator(SecureRandom random)
235
256
*/
236
257
public static NKey createServer (SecureRandom random )
237
258
throws IOException , NoSuchProviderException , NoSuchAlgorithmException {
238
- return createPair (NkeyType .SERVER , random );
259
+ return createPair (NKeyType .SERVER , random );
239
260
}
240
261
241
262
/**
@@ -250,7 +271,7 @@ public static NKey createServer(SecureRandom random)
250
271
*/
251
272
public static NKey createUser (SecureRandom random )
252
273
throws IOException , NoSuchProviderException , NoSuchAlgorithmException {
253
- return createPair (NkeyType .USER , random );
274
+ return createPair (NKeyType .USER , random );
254
275
}
255
276
256
277
/**
@@ -266,7 +287,7 @@ public static NKey fromPublicKey(char[] publicKey) {
266
287
throw new IllegalArgumentException ("Not a valid public NKey" );
267
288
}
268
289
269
- NkeyType type = NkeyType .fromPrefix (prefix );
290
+ NKeyType type = NKeyType .fromPrefix (prefix );
270
291
return new NKey (type , publicKey , null );
271
292
}
272
293
@@ -279,10 +300,10 @@ public static NKey fromSeed(char[] seed) {
279
300
NKeyDecodedSeed decoded = decodeSeed (seed ); // Should throw on bad seed
280
301
281
302
if (decoded .bytes .length == ED25519_PRIVATE_KEYSIZE ) {
282
- return new NKey (NkeyType .fromPrefix (decoded .prefix ), null , seed );
303
+ return new NKey (NKeyType .fromPrefix (decoded .prefix ), null , seed );
283
304
} else {
284
305
try {
285
- return createPair (NkeyType .fromPrefix (decoded .prefix ), decoded .bytes );
306
+ return createPair (NKeyType .fromPrefix (decoded .prefix ), decoded .bytes );
286
307
} catch (Exception e ) {
287
308
throw new IllegalArgumentException ("Bad seed value" , e );
288
309
}
@@ -294,39 +315,39 @@ public static NKey fromSeed(char[] seed) {
294
315
* @return true if the public key is an account public key
295
316
*/
296
317
public static boolean isValidPublicAccountKey (char [] src ) {
297
- return decode (NkeyType .ACCOUNT , src ) != null ;
318
+ return decode (NKeyType .ACCOUNT , src ) != null ;
298
319
}
299
320
300
321
/**
301
322
* @param src the encoded public key
302
323
* @return true if the public key is a cluster public key
303
324
*/
304
325
public static boolean isValidPublicClusterKey (char [] src ) {
305
- return decode (NkeyType .CLUSTER , src ) != null ;
326
+ return decode (NKeyType .CLUSTER , src ) != null ;
306
327
}
307
328
308
329
/**
309
330
* @param src the encoded public key
310
331
* @return true if the public key is an operator public key
311
332
*/
312
333
public static boolean isValidPublicOperatorKey (char [] src ) {
313
- return decode (NkeyType .OPERATOR , src ) != null ;
334
+ return decode (NKeyType .OPERATOR , src ) != null ;
314
335
}
315
336
316
337
/**
317
338
* @param src the encoded public key
318
339
* @return true if the public key is a server public key
319
340
*/
320
341
public static boolean isValidPublicServerKey (char [] src ) {
321
- return decode (NkeyType .SERVER , src ) != null ;
342
+ return decode (NKeyType .SERVER , src ) != null ;
322
343
}
323
344
324
345
/**
325
346
* @param src the encoded public key
326
347
* @return true if the public key is a user public key
327
348
*/
328
349
public static boolean isValidPublicUserKey (char [] src ) {
329
- return decode (NkeyType .USER , src ) != null ;
350
+ return decode (NKeyType .USER , src ) != null ;
330
351
}
331
352
332
353
/**
@@ -339,9 +360,9 @@ public static boolean isValidPublicUserKey(char[] src) {
339
360
*/
340
361
private final char [] publicKey ;
341
362
342
- private final NkeyType type ;
363
+ private final NKeyType type ;
343
364
344
- private NKey (NkeyType t , char [] publicKey , char [] privateKey ) {
365
+ private NKey (NKeyType t , char [] publicKey , char [] privateKey ) {
345
366
this .type = t ;
346
367
this .privateKeyAsSeed = privateKey ;
347
368
this .publicKey = publicKey ;
@@ -378,7 +399,7 @@ public char[] getSeed() {
378
399
byte [] seedBytes = new byte [ED25519_SEED_SIZE ];
379
400
System .arraycopy (decoded .bytes , 0 , seedBytes , 0 , seedBytes .length );
380
401
try {
381
- return encodeSeed (NkeyType .fromPrefix (decoded .prefix ), seedBytes );
402
+ return encodeSeed (NKeyType .fromPrefix (decoded .prefix ), seedBytes );
382
403
} catch (Exception e ) {
383
404
throw new IllegalStateException ("Unable to create seed." , e );
384
405
}
@@ -415,7 +436,7 @@ public char[] getPrivateKey() throws GeneralSecurityException, IOException {
415
436
}
416
437
417
438
NKeyDecodedSeed decoded = decodeSeed (privateKeyAsSeed );
418
- return encode (NkeyType .PRIVATE , decoded .bytes );
439
+ return encode (NKeyType .PRIVATE , decoded .bytes );
419
440
}
420
441
421
442
/**
@@ -448,7 +469,7 @@ public KeyPair getKeyPair() throws GeneralSecurityException, IOException {
448
469
/**
449
470
* @return the Type of this NKey
450
471
*/
451
- public NkeyType getType () {
472
+ public NKeyType getType () {
452
473
return type ;
453
474
}
454
475
0 commit comments