11
11
// See the License for the specific language governing permissions and
12
12
// limitations under the License.
13
13
14
- package nats . io .nkeys ;
14
+ package io . nats .nkeys ;
15
15
16
16
import net .i2p .crypto .eddsa .EdDSAEngine ;
17
17
import net .i2p .crypto .eddsa .EdDSAPrivateKey ;
26
26
import java .security .*;
27
27
import java .util .Arrays ;
28
28
29
- import static nats .io .nkeys .Common .*;
30
-
31
29
/**
32
30
* <p>
33
31
* The NATS ecosystem will be moving to Ed25519 keys for identity,
91
89
public class NKey {
92
90
private static boolean notValidPublicPrefixByte (int prefix ) {
93
91
switch (prefix ) {
94
- case PREFIX_BYTE_SERVER :
95
- case PREFIX_BYTE_CLUSTER :
96
- case PREFIX_BYTE_OPERATOR :
97
- case PREFIX_BYTE_ACCOUNT :
98
- case PREFIX_BYTE_USER :
92
+ case Common . PREFIX_BYTE_SERVER :
93
+ case Common . PREFIX_BYTE_CLUSTER :
94
+ case Common . PREFIX_BYTE_OPERATOR :
95
+ case Common . PREFIX_BYTE_ACCOUNT :
96
+ case Common . PREFIX_BYTE_USER :
99
97
return false ;
100
98
}
101
99
return true ;
@@ -123,23 +121,23 @@ static char[] encode(NKeyType type, byte[] src) throws IOException {
123
121
bytes .write (type .prefix );
124
122
bytes .write (src );
125
123
126
- int crc = crc16 (bytes .toByteArray ());
124
+ int crc = Common . crc16 (bytes .toByteArray ());
127
125
byte [] littleEndian = ByteBuffer .allocate (2 ).order (ByteOrder .LITTLE_ENDIAN ).putShort ((short ) crc ).array ();
128
126
129
127
bytes .write (littleEndian );
130
128
131
- char [] withPad = base32Encode (bytes .toByteArray ());
129
+ char [] withPad = Common . base32Encode (bytes .toByteArray ());
132
130
return removePaddingAndClear (withPad );
133
131
}
134
132
135
133
static char [] encodeSeed (NKeyType type , byte [] src ) throws IOException {
136
- if (src .length != ED25519_PRIVATE_KEYSIZE && src .length != ED25519_SEED_SIZE ) {
134
+ if (src .length != Common . ED25519_PRIVATE_KEYSIZE && src .length != Common . ED25519_SEED_SIZE ) {
137
135
throw new IllegalArgumentException ("Source is not the correct size for an ED25519 seed" );
138
136
}
139
137
140
138
// In order to make this human printable for both bytes, we need to do a little
141
139
// bit manipulation to setup for base32 encoding which takes 5 bits at a time.
142
- int b1 = PREFIX_BYTE_SEED | (type .prefix >> 5 );
140
+ int b1 = Common . PREFIX_BYTE_SEED | (type .prefix >> 5 );
143
141
int b2 = (type .prefix & 31 ) << 3 ; // 31 = 00011111
144
142
145
143
ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
@@ -148,17 +146,17 @@ static char[] encodeSeed(NKeyType type, byte[] src) throws IOException {
148
146
bytes .write (b2 );
149
147
bytes .write (src );
150
148
151
- int crc = crc16 (bytes .toByteArray ());
149
+ int crc = Common . crc16 (bytes .toByteArray ());
152
150
byte [] littleEndian = ByteBuffer .allocate (2 ).order (ByteOrder .LITTLE_ENDIAN ).putShort ((short ) crc ).array ();
153
151
154
152
bytes .write (littleEndian );
155
153
156
- char [] withPad = base32Encode (bytes .toByteArray ());
154
+ char [] withPad = Common . base32Encode (bytes .toByteArray ());
157
155
return removePaddingAndClear (withPad );
158
156
}
159
157
160
158
static byte [] decode (char [] src ) {
161
- byte [] raw = base32Decode (src );
159
+ byte [] raw = Common . base32Decode (src );
162
160
163
161
if (raw .length < 4 ) {
164
162
throw new IllegalArgumentException ("Invalid encoding for source string" );
@@ -168,7 +166,7 @@ static byte[] decode(char[] src) {
168
166
byte [] dataBytes = Arrays .copyOfRange (raw , 0 , raw .length - 2 );
169
167
170
168
int crc = ByteBuffer .wrap (crcBytes ).order (ByteOrder .LITTLE_ENDIAN ).getShort () & 0xFFFF ;
171
- int actual = crc16 (dataBytes );
169
+ int actual = Common . crc16 (dataBytes );
172
170
173
171
if (actual != crc ) {
174
172
throw new IllegalArgumentException ("CRC is invalid" );
@@ -199,7 +197,7 @@ static DecodedSeed decodeSeed(char[] seed) {
199
197
int b1 = raw [0 ] & 248 ; // 248 = 11111000
200
198
int b2 = (raw [0 ] & 7 ) << 5 | ((raw [1 ] & 248 ) >> 3 ); // 7 = 00000111
201
199
202
- if (b1 != PREFIX_BYTE_SEED ) {
200
+ if (b1 != Common . PREFIX_BYTE_SEED ) {
203
201
throw new IllegalArgumentException ("Invalid encoding" );
204
202
}
205
203
@@ -214,20 +212,20 @@ static DecodedSeed decodeSeed(char[] seed) {
214
212
private static NKey createPair (NKeyType type , SecureRandom random )
215
213
throws IOException , NoSuchProviderException , NoSuchAlgorithmException {
216
214
if (random == null ) {
217
- random = SRAND ;
215
+ random = Common . SRAND ;
218
216
}
219
217
220
- byte [] seed = new byte [ED_25519 .getCurve ().getField ().getb () / 8 ];
218
+ byte [] seed = new byte [Common . ED_25519 .getCurve ().getField ().getb () / 8 ];
221
219
random .nextBytes (seed );
222
220
223
221
return createPair (type , seed );
224
222
}
225
223
226
224
private static NKey createPair (NKeyType type , byte [] seed )
227
225
throws IOException , NoSuchProviderException , NoSuchAlgorithmException {
228
- EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec (seed , ED_25519 );
226
+ EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec (seed , Common . ED_25519 );
229
227
EdDSAPrivateKey privKey = new EdDSAPrivateKey (privKeySpec );
230
- EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec (privKey .getA (), ED_25519 );
228
+ EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec (privKey .getA (), Common . ED_25519 );
231
229
EdDSAPublicKey pubKey = new EdDSAPublicKey (pubKeySpec );
232
230
byte [] pubBytes = pubKey .getAbyte ();
233
231
@@ -339,7 +337,7 @@ public static NKey fromPublicKey(char[] publicKey) {
339
337
public static NKey fromSeed (char [] seed ) {
340
338
DecodedSeed decoded = decodeSeed (seed ); // Should throw on bad seed
341
339
342
- if (decoded .bytes .length == ED25519_PRIVATE_KEYSIZE ) {
340
+ if (decoded .bytes .length == Common . ED25519_PRIVATE_KEYSIZE ) {
343
341
return new NKey (NKeyType .fromPrefix (decoded .prefix ), null , seed );
344
342
} else {
345
343
try {
@@ -416,13 +414,13 @@ private NKey(NKeyType t, char[] publicKey, char[] privateKey) {
416
414
public void clear () {
417
415
if (privateKeyAsSeed != null ) {
418
416
for (int i =0 ; i < privateKeyAsSeed .length ; i ++) {
419
- privateKeyAsSeed [i ] = (char )(PRAND .nextInt (26 ) + 'a' );
417
+ privateKeyAsSeed [i ] = (char )(Common . PRAND .nextInt (26 ) + 'a' );
420
418
}
421
419
Arrays .fill (privateKeyAsSeed , '\0' );
422
420
}
423
421
if (publicKey != null ) {
424
422
for (int i =0 ; i < publicKey .length ; i ++) {
425
- publicKey [i ] = (char )(PRAND .nextInt (26 ) + 'a' );
423
+ publicKey [i ] = (char )(Common . PRAND .nextInt (26 ) + 'a' );
426
424
}
427
425
Arrays .fill (publicKey , '\0' );
428
426
}
@@ -436,7 +434,7 @@ public char[] getSeed() {
436
434
throw new IllegalStateException ("Public-only NKey" );
437
435
}
438
436
DecodedSeed decoded = decodeSeed (privateKeyAsSeed );
439
- byte [] seedBytes = new byte [ED25519_SEED_SIZE ];
437
+ byte [] seedBytes = new byte [Common . ED25519_SEED_SIZE ];
440
438
System .arraycopy (decoded .bytes , 0 , seedBytes , 0 , seedBytes .length );
441
439
try {
442
440
return encodeSeed (NKeyType .fromPrefix (decoded .prefix ), seedBytes );
@@ -492,15 +490,15 @@ public KeyPair getKeyPair() throws GeneralSecurityException, IOException {
492
490
}
493
491
494
492
DecodedSeed decoded = decodeSeed (privateKeyAsSeed );
495
- byte [] seedBytes = new byte [ED25519_SEED_SIZE ];
496
- byte [] pubBytes = new byte [ED25519_PUBLIC_KEYSIZE ];
493
+ byte [] seedBytes = new byte [Common . ED25519_SEED_SIZE ];
494
+ byte [] pubBytes = new byte [Common . ED25519_PUBLIC_KEYSIZE ];
497
495
498
496
System .arraycopy (decoded .bytes , 0 , seedBytes , 0 , seedBytes .length );
499
497
System .arraycopy (decoded .bytes , seedBytes .length , pubBytes , 0 , pubBytes .length );
500
498
501
- EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec (seedBytes , ED_25519 );
499
+ EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec (seedBytes , Common . ED_25519 );
502
500
EdDSAPrivateKey privKey = new EdDSAPrivateKey (privKeySpec );
503
- EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec (pubBytes , ED_25519 );
501
+ EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec (pubBytes , Common . ED_25519 );
504
502
EdDSAPublicKey pubKey = new EdDSAPublicKey (pubKeySpec );
505
503
506
504
return new KeyPair (pubKey , privKey );
@@ -523,7 +521,7 @@ public NKeyType getType() {
523
521
* @throws IOException if there is a problem reading the data
524
522
*/
525
523
public byte [] sign (byte [] input ) throws GeneralSecurityException , IOException {
526
- Signature sgr = new EdDSAEngine (MessageDigest .getInstance (ED_25519 .getHashAlgorithm ()));
524
+ Signature sgr = new EdDSAEngine (MessageDigest .getInstance (Common . ED_25519 .getHashAlgorithm ()));
527
525
PrivateKey sKey = getKeyPair ().getPrivate ();
528
526
529
527
sgr .initSign (sKey );
@@ -543,15 +541,15 @@ public byte[] sign(byte[] input) throws GeneralSecurityException, IOException {
543
541
* @throws IOException if there is a problem reading the data
544
542
*/
545
543
public boolean verify (byte [] input , byte [] signature ) throws GeneralSecurityException , IOException {
546
- Signature sgr = new EdDSAEngine (MessageDigest .getInstance (ED_25519 .getHashAlgorithm ()));
544
+ Signature sgr = new EdDSAEngine (MessageDigest .getInstance (Common . ED_25519 .getHashAlgorithm ()));
547
545
PublicKey sKey = null ;
548
546
549
547
if (privateKeyAsSeed != null ) {
550
548
sKey = getKeyPair ().getPublic ();
551
549
} else {
552
550
char [] encodedPublicKey = getPublicKey ();
553
551
byte [] decodedPublicKey = decode (this .type , encodedPublicKey , false );
554
- EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec (decodedPublicKey , ED_25519 );
552
+ EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec (decodedPublicKey , Common . ED_25519 );
555
553
sKey = new EdDSAPublicKey (pubKeySpec );
556
554
}
557
555
0 commit comments