Skip to content

Commit dc50a9b

Browse files
authored
Merge pull request #2 from nats-io/package-fix
Fix package
2 parents 0ea288d + d868645 commit dc50a9b

File tree

7 files changed

+52
-57
lines changed

7 files changed

+52
-57
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 = "1.0.0"
14+
def jarVersion = "1.1.0"
1515
group = 'io.nats'
1616

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

src/main/java/nats/io/nkeys/Common.java src/main/java/io/nats/nkeys/Common.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package nats.io.nkeys;
14+
package io.nats.nkeys;
1515

1616
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveSpec;
1717
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;

src/main/java/nats/io/nkeys/DecodedSeed.java src/main/java/io/nats/nkeys/DecodedSeed.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package nats.io.nkeys;
14+
package io.nats.nkeys;
1515

1616
class DecodedSeed {
1717
public final int prefix;

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

+30-32
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package nats.io.nkeys;
14+
package io.nats.nkeys;
1515

1616
import net.i2p.crypto.eddsa.EdDSAEngine;
1717
import net.i2p.crypto.eddsa.EdDSAPrivateKey;
@@ -26,8 +26,6 @@
2626
import java.security.*;
2727
import java.util.Arrays;
2828

29-
import static nats.io.nkeys.Common.*;
30-
3129
/**
3230
* <p>
3331
* The NATS ecosystem will be moving to Ed25519 keys for identity,
@@ -91,11 +89,11 @@
9189
public class NKey {
9290
private static boolean notValidPublicPrefixByte(int prefix) {
9391
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:
9997
return false;
10098
}
10199
return true;
@@ -123,23 +121,23 @@ static char[] encode(NKeyType type, byte[] src) throws IOException {
123121
bytes.write(type.prefix);
124122
bytes.write(src);
125123

126-
int crc = crc16(bytes.toByteArray());
124+
int crc = Common.crc16(bytes.toByteArray());
127125
byte[] littleEndian = ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort((short) crc).array();
128126

129127
bytes.write(littleEndian);
130128

131-
char[] withPad = base32Encode(bytes.toByteArray());
129+
char[] withPad = Common.base32Encode(bytes.toByteArray());
132130
return removePaddingAndClear(withPad);
133131
}
134132

135133
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) {
137135
throw new IllegalArgumentException("Source is not the correct size for an ED25519 seed");
138136
}
139137

140138
// In order to make this human printable for both bytes, we need to do a little
141139
// 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);
143141
int b2 = (type.prefix & 31) << 3; // 31 = 00011111
144142

145143
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
@@ -148,17 +146,17 @@ static char[] encodeSeed(NKeyType type, byte[] src) throws IOException {
148146
bytes.write(b2);
149147
bytes.write(src);
150148

151-
int crc = crc16(bytes.toByteArray());
149+
int crc = Common.crc16(bytes.toByteArray());
152150
byte[] littleEndian = ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort((short) crc).array();
153151

154152
bytes.write(littleEndian);
155153

156-
char[] withPad = base32Encode(bytes.toByteArray());
154+
char[] withPad = Common.base32Encode(bytes.toByteArray());
157155
return removePaddingAndClear(withPad);
158156
}
159157

160158
static byte[] decode(char[] src) {
161-
byte[] raw = base32Decode(src);
159+
byte[] raw = Common.base32Decode(src);
162160

163161
if (raw.length < 4) {
164162
throw new IllegalArgumentException("Invalid encoding for source string");
@@ -168,7 +166,7 @@ static byte[] decode(char[] src) {
168166
byte[] dataBytes = Arrays.copyOfRange(raw, 0, raw.length - 2);
169167

170168
int crc = ByteBuffer.wrap(crcBytes).order(ByteOrder.LITTLE_ENDIAN).getShort() & 0xFFFF;
171-
int actual = crc16(dataBytes);
169+
int actual = Common.crc16(dataBytes);
172170

173171
if (actual != crc) {
174172
throw new IllegalArgumentException("CRC is invalid");
@@ -199,7 +197,7 @@ static DecodedSeed decodeSeed(char[] seed) {
199197
int b1 = raw[0] & 248; // 248 = 11111000
200198
int b2 = (raw[0] & 7) << 5 | ((raw[1] & 248) >> 3); // 7 = 00000111
201199

202-
if (b1 != PREFIX_BYTE_SEED) {
200+
if (b1 != Common.PREFIX_BYTE_SEED) {
203201
throw new IllegalArgumentException("Invalid encoding");
204202
}
205203

@@ -214,20 +212,20 @@ static DecodedSeed decodeSeed(char[] seed) {
214212
private static NKey createPair(NKeyType type, SecureRandom random)
215213
throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
216214
if (random == null) {
217-
random = SRAND;
215+
random = Common.SRAND;
218216
}
219217

220-
byte[] seed = new byte[ED_25519.getCurve().getField().getb() / 8];
218+
byte[] seed = new byte[Common.ED_25519.getCurve().getField().getb() / 8];
221219
random.nextBytes(seed);
222220

223221
return createPair(type, seed);
224222
}
225223

226224
private static NKey createPair(NKeyType type, byte[] seed)
227225
throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
228-
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(seed, ED_25519);
226+
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(seed, Common.ED_25519);
229227
EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec);
230-
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ED_25519);
228+
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), Common.ED_25519);
231229
EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec);
232230
byte[] pubBytes = pubKey.getAbyte();
233231

@@ -339,7 +337,7 @@ public static NKey fromPublicKey(char[] publicKey) {
339337
public static NKey fromSeed(char[] seed) {
340338
DecodedSeed decoded = decodeSeed(seed); // Should throw on bad seed
341339

342-
if (decoded.bytes.length == ED25519_PRIVATE_KEYSIZE) {
340+
if (decoded.bytes.length == Common.ED25519_PRIVATE_KEYSIZE) {
343341
return new NKey(NKeyType.fromPrefix(decoded.prefix), null, seed);
344342
} else {
345343
try {
@@ -416,13 +414,13 @@ private NKey(NKeyType t, char[] publicKey, char[] privateKey) {
416414
public void clear() {
417415
if (privateKeyAsSeed != null) {
418416
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');
420418
}
421419
Arrays.fill(privateKeyAsSeed, '\0');
422420
}
423421
if (publicKey != null) {
424422
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');
426424
}
427425
Arrays.fill(publicKey, '\0');
428426
}
@@ -436,7 +434,7 @@ public char[] getSeed() {
436434
throw new IllegalStateException("Public-only NKey");
437435
}
438436
DecodedSeed decoded = decodeSeed(privateKeyAsSeed);
439-
byte[] seedBytes = new byte[ED25519_SEED_SIZE];
437+
byte[] seedBytes = new byte[Common.ED25519_SEED_SIZE];
440438
System.arraycopy(decoded.bytes, 0, seedBytes, 0, seedBytes.length);
441439
try {
442440
return encodeSeed(NKeyType.fromPrefix(decoded.prefix), seedBytes);
@@ -492,15 +490,15 @@ public KeyPair getKeyPair() throws GeneralSecurityException, IOException {
492490
}
493491

494492
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];
497495

498496
System.arraycopy(decoded.bytes, 0, seedBytes, 0, seedBytes.length);
499497
System.arraycopy(decoded.bytes, seedBytes.length, pubBytes, 0, pubBytes.length);
500498

501-
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(seedBytes, ED_25519);
499+
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(seedBytes, Common.ED_25519);
502500
EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec);
503-
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(pubBytes, ED_25519);
501+
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(pubBytes, Common.ED_25519);
504502
EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec);
505503

506504
return new KeyPair(pubKey, privKey);
@@ -523,7 +521,7 @@ public NKeyType getType() {
523521
* @throws IOException if there is a problem reading the data
524522
*/
525523
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()));
527525
PrivateKey sKey = getKeyPair().getPrivate();
528526

529527
sgr.initSign(sKey);
@@ -543,15 +541,15 @@ public byte[] sign(byte[] input) throws GeneralSecurityException, IOException {
543541
* @throws IOException if there is a problem reading the data
544542
*/
545543
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()));
547545
PublicKey sKey = null;
548546

549547
if (privateKeyAsSeed != null) {
550548
sKey = getKeyPair().getPublic();
551549
} else {
552550
char[] encodedPublicKey = getPublicKey();
553551
byte[] decodedPublicKey = decode(this.type, encodedPublicKey, false);
554-
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(decodedPublicKey, ED_25519);
552+
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(decodedPublicKey, Common.ED_25519);
555553
sKey = new EdDSAPublicKey(pubKeySpec);
556554
}
557555

src/main/java/nats/io/nkeys/NKeyType.java src/main/java/io/nats/nkeys/NKeyType.java

+13-15
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package nats.io.nkeys;
15-
16-
import static nats.io.nkeys.Common.*;
14+
package io.nats.nkeys;
1715

1816
/**
1917
* NKeys use a prefix byte to indicate their intended owner: 'N' = server, 'C' =
@@ -22,17 +20,17 @@
2220
*/
2321
public enum NKeyType {
2422
/** A user NKey. */
25-
USER(PREFIX_BYTE_USER),
23+
USER(Common.PREFIX_BYTE_USER),
2624
/** An account NKey. */
27-
ACCOUNT(PREFIX_BYTE_ACCOUNT),
25+
ACCOUNT(Common.PREFIX_BYTE_ACCOUNT),
2826
/** A server NKey. */
29-
SERVER(PREFIX_BYTE_SERVER),
27+
SERVER(Common.PREFIX_BYTE_SERVER),
3028
/** An operator NKey. */
31-
OPERATOR(PREFIX_BYTE_OPERATOR),
29+
OPERATOR(Common.PREFIX_BYTE_OPERATOR),
3230
/** A cluster NKey. */
33-
CLUSTER(PREFIX_BYTE_CLUSTER),
31+
CLUSTER(Common.PREFIX_BYTE_CLUSTER),
3432
/** A private NKey. */
35-
PRIVATE(PREFIX_BYTE_PRIVATE);
33+
PRIVATE(Common.PREFIX_BYTE_PRIVATE);
3634

3735
public final int prefix;
3836

@@ -41,17 +39,17 @@ public enum NKeyType {
4139
}
4240

4341
public static NKeyType fromPrefix(int prefix) {
44-
if (prefix == PREFIX_BYTE_ACCOUNT) {
42+
if (prefix == Common.PREFIX_BYTE_ACCOUNT) {
4543
return ACCOUNT;
46-
} else if (prefix == PREFIX_BYTE_SERVER) {
44+
} else if (prefix == Common.PREFIX_BYTE_SERVER) {
4745
return SERVER;
48-
} else if (prefix == PREFIX_BYTE_USER) {
46+
} else if (prefix == Common.PREFIX_BYTE_USER) {
4947
return USER;
50-
} else if (prefix == PREFIX_BYTE_CLUSTER) {
48+
} else if (prefix == Common.PREFIX_BYTE_CLUSTER) {
5149
return CLUSTER;
52-
} else if (prefix == PREFIX_BYTE_PRIVATE) {
50+
} else if (prefix == Common.PREFIX_BYTE_PRIVATE) {
5351
return ACCOUNT;
54-
} else if (prefix == PREFIX_BYTE_OPERATOR) {
52+
} else if (prefix == Common.PREFIX_BYTE_OPERATOR) {
5553
return OPERATOR;
5654
}
5755

src/test/java/nats/io/ResourceUtils.java src/test/java/io/ResourceUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package nats.io;
1+
package io;
22

33
import java.io.File;
44
import java.nio.file.Files;

src/test/java/nats/io/nkeys/NKeyTests.java src/test/java/io/nats/nkeys/NKeyTests.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package nats.io.nkeys;
14+
package io.nats.nkeys;
1515

16-
import nats.io.ResourceUtils;
16+
import io.ResourceUtils;
1717
import org.junit.jupiter.api.Test;
1818

1919
import java.nio.charset.StandardCharsets;
@@ -22,9 +22,8 @@
2222
import java.util.Base64;
2323
import java.util.List;
2424

25-
import static nats.io.ResourceUtils.resourceAsLines;
26-
import static nats.io.nkeys.Common.*;
27-
import static nats.io.nkeys.NKey.removePaddingAndClear;
25+
import static io.nats.nkeys.Common.*;
26+
import static io.nats.nkeys.NKey.removePaddingAndClear;
2827
import static org.junit.jupiter.api.Assertions.*;
2928

3029
public class NKeyTests {
@@ -69,7 +68,7 @@ public void testCRC16() {
6968

7069
@Test
7170
public void testBase32() {
72-
List<String> inputs = resourceAsLines("utf8-test-strings.txt");
71+
List<String> inputs = ResourceUtils.resourceAsLines("utf8-test-strings.txt");
7372

7473
for (String expected : inputs) {
7574
byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)