Skip to content

Commit 92be4ad

Browse files
authored
Merge pull request #13 from nats-io/fixed-name-case
fixed class name
2 parents 3937525 + ecf3286 commit 92be4ad

File tree

4 files changed

+110
-67
lines changed

4 files changed

+110
-67
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111
id 'signing'
1212
}
1313

14-
def jarVersion = "2.0.0"
14+
def jarVersion = "2.0.1"
1515
group = 'io.nats'
1616

1717
def isMerge = System.getenv("BUILD_EVENT") == "push"

src/main/java/io/nats/nkey/NKey.java

+53-32
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,42 @@
1313

1414
package io.nats.nkey;
1515

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;
2131

2232
import java.io.ByteArrayOutputStream;
2333
import java.io.IOException;
2434
import java.nio.ByteBuffer;
2535
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;
2745
import java.util.Arrays;
2846

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;
3152

3253
public class NKey {
3354

@@ -59,7 +80,7 @@ static char[] removePaddingAndClear(char[] withPad) {
5980
return withoutPad;
6081
}
6182

62-
static char[] encode(NkeyType type, byte[] src) throws IOException {
83+
static char[] encode(NKeyType type, byte[] src) throws IOException {
6384
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
6485

6586
bytes.write(type.prefix);
@@ -74,7 +95,7 @@ static char[] encode(NkeyType type, byte[] src) throws IOException {
7495
return removePaddingAndClear(withPad);
7596
}
7697

77-
static char[] encodeSeed(NkeyType type, byte[] src) throws IOException {
98+
static char[] encodeSeed(NKeyType type, byte[] src) throws IOException {
7899
if (src.length != ED25519_PRIVATE_KEYSIZE && src.length != ED25519_SEED_SIZE) {
79100
throw new IllegalArgumentException("Source is not the correct size for an ED25519 seed");
80101
}
@@ -119,10 +140,10 @@ static byte[] decode(char[] src) {
119140
return dataBytes;
120141
}
121142

122-
static byte[] decode(NkeyType expectedType, char[] src) {
143+
static byte[] decode(NKeyType expectedType, char[] src) {
123144
byte[] raw = decode(src);
124145
byte[] dataBytes = Arrays.copyOfRange(raw, 1, raw.length);
125-
NkeyType type = NkeyType.fromPrefix(raw[0] & 0xFF);
146+
NKeyType type = NKeyType.fromPrefix(raw[0] & 0xFF);
126147

127148
if (type != expectedType) {
128149
return null;
@@ -150,7 +171,7 @@ static NKeyDecodedSeed decodeSeed(char[] seed) {
150171
return new NKeyDecodedSeed(b2, dataBytes);
151172
}
152173

153-
private static NKey createPair(NkeyType type, SecureRandom random)
174+
private static NKey createPair(NKeyType type, SecureRandom random)
154175
throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
155176
if (random == null) {
156177
random = SRAND;
@@ -162,7 +183,7 @@ private static NKey createPair(NkeyType type, SecureRandom random)
162183
return createPair(type, seed);
163184
}
164185

165-
private static NKey createPair(NkeyType type, byte[] seed)
186+
private static NKey createPair(NKeyType type, byte[] seed)
166187
throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
167188
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(seed, ED_25519);
168189
EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec);
@@ -190,7 +211,7 @@ private static NKey createPair(NkeyType type, byte[] seed)
190211
*/
191212
public static NKey createAccount(SecureRandom random)
192213
throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
193-
return createPair(NkeyType.ACCOUNT, random);
214+
return createPair(NKeyType.ACCOUNT, random);
194215
}
195216

196217
/**
@@ -205,7 +226,7 @@ public static NKey createAccount(SecureRandom random)
205226
*/
206227
public static NKey createCluster(SecureRandom random)
207228
throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
208-
return createPair(NkeyType.CLUSTER, random);
229+
return createPair(NKeyType.CLUSTER, random);
209230
}
210231

211232
/**
@@ -220,7 +241,7 @@ public static NKey createCluster(SecureRandom random)
220241
*/
221242
public static NKey createOperator(SecureRandom random)
222243
throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
223-
return createPair(NkeyType.OPERATOR, random);
244+
return createPair(NKeyType.OPERATOR, random);
224245
}
225246

226247
/**
@@ -235,7 +256,7 @@ public static NKey createOperator(SecureRandom random)
235256
*/
236257
public static NKey createServer(SecureRandom random)
237258
throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
238-
return createPair(NkeyType.SERVER, random);
259+
return createPair(NKeyType.SERVER, random);
239260
}
240261

241262
/**
@@ -250,7 +271,7 @@ public static NKey createServer(SecureRandom random)
250271
*/
251272
public static NKey createUser(SecureRandom random)
252273
throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
253-
return createPair(NkeyType.USER, random);
274+
return createPair(NKeyType.USER, random);
254275
}
255276

256277
/**
@@ -266,7 +287,7 @@ public static NKey fromPublicKey(char[] publicKey) {
266287
throw new IllegalArgumentException("Not a valid public NKey");
267288
}
268289

269-
NkeyType type = NkeyType.fromPrefix(prefix);
290+
NKeyType type = NKeyType.fromPrefix(prefix);
270291
return new NKey(type, publicKey, null);
271292
}
272293

@@ -279,10 +300,10 @@ public static NKey fromSeed(char[] seed) {
279300
NKeyDecodedSeed decoded = decodeSeed(seed); // Should throw on bad seed
280301

281302
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);
283304
} else {
284305
try {
285-
return createPair(NkeyType.fromPrefix(decoded.prefix), decoded.bytes);
306+
return createPair(NKeyType.fromPrefix(decoded.prefix), decoded.bytes);
286307
} catch (Exception e) {
287308
throw new IllegalArgumentException("Bad seed value", e);
288309
}
@@ -294,39 +315,39 @@ public static NKey fromSeed(char[] seed) {
294315
* @return true if the public key is an account public key
295316
*/
296317
public static boolean isValidPublicAccountKey(char[] src) {
297-
return decode(NkeyType.ACCOUNT, src) != null;
318+
return decode(NKeyType.ACCOUNT, src) != null;
298319
}
299320

300321
/**
301322
* @param src the encoded public key
302323
* @return true if the public key is a cluster public key
303324
*/
304325
public static boolean isValidPublicClusterKey(char[] src) {
305-
return decode(NkeyType.CLUSTER, src) != null;
326+
return decode(NKeyType.CLUSTER, src) != null;
306327
}
307328

308329
/**
309330
* @param src the encoded public key
310331
* @return true if the public key is an operator public key
311332
*/
312333
public static boolean isValidPublicOperatorKey(char[] src) {
313-
return decode(NkeyType.OPERATOR, src) != null;
334+
return decode(NKeyType.OPERATOR, src) != null;
314335
}
315336

316337
/**
317338
* @param src the encoded public key
318339
* @return true if the public key is a server public key
319340
*/
320341
public static boolean isValidPublicServerKey(char[] src) {
321-
return decode(NkeyType.SERVER, src) != null;
342+
return decode(NKeyType.SERVER, src) != null;
322343
}
323344

324345
/**
325346
* @param src the encoded public key
326347
* @return true if the public key is a user public key
327348
*/
328349
public static boolean isValidPublicUserKey(char[] src) {
329-
return decode(NkeyType.USER, src) != null;
350+
return decode(NKeyType.USER, src) != null;
330351
}
331352

332353
/**
@@ -339,9 +360,9 @@ public static boolean isValidPublicUserKey(char[] src) {
339360
*/
340361
private final char[] publicKey;
341362

342-
private final NkeyType type;
363+
private final NKeyType type;
343364

344-
private NKey(NkeyType t, char[] publicKey, char[] privateKey) {
365+
private NKey(NKeyType t, char[] publicKey, char[] privateKey) {
345366
this.type = t;
346367
this.privateKeyAsSeed = privateKey;
347368
this.publicKey = publicKey;
@@ -378,7 +399,7 @@ public char[] getSeed() {
378399
byte[] seedBytes = new byte[ED25519_SEED_SIZE];
379400
System.arraycopy(decoded.bytes, 0, seedBytes, 0, seedBytes.length);
380401
try {
381-
return encodeSeed(NkeyType.fromPrefix(decoded.prefix), seedBytes);
402+
return encodeSeed(NKeyType.fromPrefix(decoded.prefix), seedBytes);
382403
} catch (Exception e) {
383404
throw new IllegalStateException("Unable to create seed.", e);
384405
}
@@ -415,7 +436,7 @@ public char[] getPrivateKey() throws GeneralSecurityException, IOException {
415436
}
416437

417438
NKeyDecodedSeed decoded = decodeSeed(privateKeyAsSeed);
418-
return encode(NkeyType.PRIVATE, decoded.bytes);
439+
return encode(NKeyType.PRIVATE, decoded.bytes);
419440
}
420441

421442
/**
@@ -448,7 +469,7 @@ public KeyPair getKeyPair() throws GeneralSecurityException, IOException {
448469
/**
449470
* @return the Type of this NKey
450471
*/
451-
public NkeyType getType() {
472+
public NKeyType getType() {
452473
return type;
453474
}
454475

src/main/java/io/nats/nkey/NkeyType.java src/main/java/io/nats/nkey/NKeyType.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313

1414
package io.nats.nkey;
1515

16-
import static io.nats.nkey.NKeyConstants.*;
16+
import static io.nats.nkey.NKeyConstants.PREFIX_BYTE_ACCOUNT;
17+
import static io.nats.nkey.NKeyConstants.PREFIX_BYTE_CLUSTER;
18+
import static io.nats.nkey.NKeyConstants.PREFIX_BYTE_OPERATOR;
19+
import static io.nats.nkey.NKeyConstants.PREFIX_BYTE_PRIVATE;
20+
import static io.nats.nkey.NKeyConstants.PREFIX_BYTE_SERVER;
21+
import static io.nats.nkey.NKeyConstants.PREFIX_BYTE_USER;
1722

1823
/**
1924
* NKeys use a prefix byte to indicate their intended owner: 'N' = server, 'C' =
2025
* cluster, 'A' = account, and 'U' = user. 'P' is used for private keys. The
2126
* NKey class formalizes these into the enum Type.
2227
*/
23-
public enum NkeyType {
28+
public enum NKeyType {
2429
/**
2530
* A user NKey.
2631
*/
@@ -48,11 +53,11 @@ public enum NkeyType {
4853

4954
public final int prefix;
5055

51-
NkeyType(int prefix) {
56+
NKeyType(int prefix) {
5257
this.prefix = prefix;
5358
}
5459

55-
public static NkeyType fromPrefix(int prefix) {
60+
public static NKeyType fromPrefix(int prefix) {
5661
switch (prefix) {
5762
case PREFIX_BYTE_ACCOUNT:
5863
case PREFIX_BYTE_PRIVATE: return ACCOUNT;

0 commit comments

Comments
 (0)