Skip to content

Commit f15eb09

Browse files
authored
Merge pull request #25 from nats-io/base32-encoding-public
Make all utils public
2 parents 4f28113 + b7b56bc commit f15eb09

File tree

7 files changed

+119
-62
lines changed

7 files changed

+119
-62
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.util.Arrays;
1818

1919
import static io.nats.nkey.NKeyConstants.ED25519_SEED_SIZE;
20-
import static io.nats.nkey.NKeyInternalUtils.*;
20+
import static io.nats.nkey.NKeyProviderUtils.*;
2121

2222
/**
2323
* The NKey class
@@ -132,7 +132,7 @@ public char[] getPublicKey() {
132132
if (publicKey != null) {
133133
return publicKey;
134134
}
135-
return encode(type, getKeyPair().getPublic().getEncoded());
135+
return nkeyEncode(type, getKeyPair().getPublic().getEncoded());
136136
}
137137

138138
/**
@@ -141,7 +141,7 @@ public char[] getPublicKey() {
141141
*/
142142
public char[] getPrivateKey() {
143143
NKeyDecodedSeed decoded = getDecodedSeed();
144-
return encode(NKeyType.PRIVATE, decoded.bytes);
144+
return nkeyEncode(NKeyType.PRIVATE, decoded.bytes);
145145
}
146146

147147
/**

core/src/main/java/io/nats/nkey/NKeyProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.Random;
2525

2626
import static io.nats.nkey.NKeyConstants.*;
27-
import static io.nats.nkey.NKeyInternalUtils.*;
27+
import static io.nats.nkey.NKeyProviderUtils.*;
2828

2929
/**
3030
* The NKeyProvider is the central object in this package.
@@ -226,7 +226,7 @@ public NKey createUser() {
226226
* @return the new NKey
227227
*/
228228
public NKey fromPublicKey(char[] publicKey) {
229-
byte[] raw = decode(publicKey);
229+
byte[] raw = nkeyDecode(publicKey);
230230
int prefix = raw[0] & 0xFF;
231231

232232
if (notValidPublicPrefixByte(prefix)) {

core/src/main/java/io/nats/nkey/NKeyInternalUtils.java renamed to core/src/main/java/io/nats/nkey/NKeyProviderUtils.java

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323

2424
import static io.nats.nkey.NKeyConstants.*;
2525

26-
abstract class NKeyInternalUtils {
27-
private NKeyInternalUtils() {} /* ensures cannot be constructed */
26+
/**
27+
* Provider Utils
28+
*/
29+
public abstract class NKeyProviderUtils {
30+
private NKeyProviderUtils() {} /* ensures cannot be constructed */
2831

2932
private static final String BASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
3033
private static final int MASK = 31;
@@ -41,19 +44,28 @@ private NKeyInternalUtils() {} /* ensures cannot be constructed */
4144
}
4245
}
4346

44-
static boolean notValidPublicPrefixByte(int prefix) {
45-
switch (prefix) {
46-
case PREFIX_BYTE_SERVER:
47-
case PREFIX_BYTE_CLUSTER:
48-
case PREFIX_BYTE_OPERATOR:
49-
case PREFIX_BYTE_ACCOUNT:
50-
case PREFIX_BYTE_USER:
51-
return false;
52-
}
53-
return true;
47+
/**
48+
* determine if the prefix is not a public prefix
49+
* @param prefix the prefix
50+
* @return true if the prefix is not public
51+
*/
52+
public static boolean notValidPublicPrefixByte(int prefix) {
53+
return switch (prefix) {
54+
case PREFIX_BYTE_SERVER,
55+
PREFIX_BYTE_CLUSTER,
56+
PREFIX_BYTE_OPERATOR,
57+
PREFIX_BYTE_ACCOUNT,
58+
PREFIX_BYTE_USER -> false;
59+
default -> true;
60+
};
5461
}
5562

56-
static char[] removePaddingAndClear(char[] withPad) {
63+
/**
64+
* remove padding from a base32 encoded character array
65+
* @param withPad the character array with padding
66+
* @return the character array without padding
67+
*/
68+
public static char[] removePaddingAndClear(char[] withPad) {
5769
int i;
5870
for (i = withPad.length-1; i >= 0; i--) {
5971
if (withPad[i] != '=') {
@@ -68,7 +80,13 @@ static char[] removePaddingAndClear(char[] withPad) {
6880
return withoutPad;
6981
}
7082

71-
static char[] encode(NKeyType type, byte[] src){
83+
/**
84+
* Encode to nkey format
85+
* @param type the type
86+
* @param src the seed bytes
87+
* @return the encoded characters
88+
*/
89+
public static char[] nkeyEncode(NKeyType type, byte[] src){
7290
try {
7391
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
7492

@@ -88,7 +106,13 @@ static char[] encode(NKeyType type, byte[] src){
88106
}
89107
}
90108

91-
static char[] encodeSeed(NKeyType type, byte[] src) {
109+
/**
110+
* Encode the seed
111+
* @param type the type
112+
* @param src the seed bytes
113+
* @return the encoded characters
114+
*/
115+
public static char[] encodeSeed(NKeyType type, byte[] src) {
92116
if (src.length != ED25519_PRIVATE_KEYSIZE && src.length != ED25519_SEED_SIZE) {
93117
throw new IllegalArgumentException("Source is not the correct size for an ED25519 seed");
94118
}
@@ -118,7 +142,12 @@ static char[] encodeSeed(NKeyType type, byte[] src) {
118142
}
119143
}
120144

121-
static byte[] decode(char[] src) {
145+
/**
146+
* Decode the encoded characters from NKey format
147+
* @param src the encoded characters
148+
* @return the decoded characters
149+
*/
150+
public static byte[] nkeyDecode(char[] src) {
122151
byte[] raw = base32Decode(src);
123152

124153
if (raw.length < 4) {
@@ -138,8 +167,14 @@ static byte[] decode(char[] src) {
138167
return dataBytes;
139168
}
140169

141-
static byte @NonNull [] decode(NKeyType expectedType, char[] src) {
142-
byte[] raw = decode(src);
170+
/**
171+
* Decode the encoded characters from NKey format expecting a specific type
172+
* @param expectedType the expected type of the NKey
173+
* @param src the encoded characters
174+
* @return the decoded characters
175+
*/
176+
public static byte @NonNull [] nkeyDecode(NKeyType expectedType, char[] src) {
177+
byte[] raw = nkeyDecode(src);
143178
byte[] dataBytes = Arrays.copyOfRange(raw, 1, raw.length);
144179
NKeyType type = NKeyType.fromPrefix(raw[0] & 0xFF);
145180
if (type == null) {
@@ -151,8 +186,13 @@ static byte[] decode(char[] src) {
151186
return dataBytes;
152187
}
153188

154-
static NKeyDecodedSeed decodeSeed(char[] seed) {
155-
byte[] raw = decode(seed);
189+
/**
190+
* Decode the seed
191+
* @param seed the encoded seed characters
192+
* @return the decoded bytes
193+
*/
194+
public static NKeyDecodedSeed decodeSeed(char[] seed) {
195+
byte[] raw = nkeyDecode(seed);
156196

157197
// Need to do the reverse here to get back to internal representation.
158198
int b1 = raw[0] & 248; // 248 = 11111000
@@ -170,7 +210,12 @@ static NKeyDecodedSeed decodeSeed(char[] seed) {
170210
return new NKeyDecodedSeed(b2, dataBytes);
171211
}
172212

173-
static int crc16(byte[] bytes) {
213+
/**
214+
* Calculate a crc16
215+
* @param bytes the bytes to use to calculate
216+
* @return the crc
217+
*/
218+
public static int crc16(byte[] bytes) {
174219
int crc = 0;
175220

176221
for (byte b : bytes) {
@@ -180,8 +225,13 @@ static int crc16(byte[] bytes) {
180225
return crc;
181226
}
182227

183-
// http://en.wikipedia.org/wiki/Base_32
184-
static char[] base32Encode(final byte[] input) {
228+
/**
229+
* Base 32 Encode a byte array
230+
* @see <a href="http://en.wikipedia.org/wiki/Base_32">wikipedia Base 32</a>
231+
* @param input the byte array
232+
* @return the base32 encoded character array
233+
*/
234+
public static char[] base32Encode(final byte[] input) {
185235
int last = input.length;
186236
char[] charBuff = new char[(last + 7) * 8 / SHIFT];
187237
int offset = 0;
@@ -224,7 +274,13 @@ static char[] base32Encode(final byte[] input) {
224274
return retVal;
225275
}
226276

227-
static byte[] base32Decode(final char[] input) {
277+
/**
278+
* Base 32 Decode a character array
279+
* @see <a href="http://en.wikipedia.org/wiki/Base_32">wikipedia Base 32</a>
280+
* @param input the character array
281+
* @return the decoded byte array
282+
*/
283+
public static byte[] base32Decode(final char[] input) {
228284
byte[] bytes = new byte[input.length * SHIFT / 8];
229285
int buffer = 0;
230286
int next = 0;

core/src/main/java/io/nats/nkey/NKeyUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
package io.nats.nkey;
1515

16-
import static io.nats.nkey.NKeyInternalUtils.decode;
16+
import static io.nats.nkey.NKeyProviderUtils.nkeyDecode;
1717

1818
/**
1919
* General Utils
@@ -28,7 +28,7 @@ private NKeyUtils() {} /* ensures cannot be constructed */
2828
* @throws IllegalArgumentException if is not a valid Account key
2929
*/
3030
public static boolean isValidPublicAccountKey(char[] src) {
31-
decode(NKeyType.ACCOUNT, src);
31+
nkeyDecode(NKeyType.ACCOUNT, src);
3232
return true;
3333
}
3434

@@ -39,7 +39,7 @@ public static boolean isValidPublicAccountKey(char[] src) {
3939
* @throws IllegalArgumentException if is not a valid Cluster key
4040
*/
4141
public static boolean isValidPublicClusterKey(char[] src) {
42-
decode(NKeyType.CLUSTER, src);
42+
nkeyDecode(NKeyType.CLUSTER, src);
4343
return true;
4444
}
4545

@@ -50,7 +50,7 @@ public static boolean isValidPublicClusterKey(char[] src) {
5050
* @throws IllegalArgumentException if is not a valid Operator key
5151
*/
5252
public static boolean isValidPublicOperatorKey(char[] src) {
53-
decode(NKeyType.OPERATOR, src);
53+
nkeyDecode(NKeyType.OPERATOR, src);
5454
return true;
5555
}
5656

@@ -61,7 +61,7 @@ public static boolean isValidPublicOperatorKey(char[] src) {
6161
* @throws IllegalArgumentException if is not a valid Server key
6262
*/
6363
public static boolean isValidPublicServerKey(char[] src) {
64-
decode(NKeyType.SERVER, src);
64+
nkeyDecode(NKeyType.SERVER, src);
6565
return true;
6666
}
6767

@@ -72,7 +72,7 @@ public static boolean isValidPublicServerKey(char[] src) {
7272
* @throws IllegalArgumentException if is not a valid User key
7373
*/
7474
public static boolean isValidPublicUserKey(char[] src) {
75-
decode(NKeyType.USER, src);
75+
nkeyDecode(NKeyType.USER, src);
7676
return true;
7777
}
7878
}

core/src/test/java/io/nats/nkey/CoreNKeyProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import static io.nats.nkey.NKeyConstants.ED25519_PUBLIC_KEYSIZE;
1111
import static io.nats.nkey.NKeyConstants.ED25519_SEED_SIZE;
12-
import static io.nats.nkey.NKeyInternalUtils.decode;
13-
import static io.nats.nkey.NKeyInternalUtils.encodeSeed;
12+
import static io.nats.nkey.NKeyProviderUtils.encodeSeed;
13+
import static io.nats.nkey.NKeyProviderUtils.nkeyDecode;
1414

1515
@NullMarked
1616
public class CoreNKeyProvider extends NKeyProvider {
@@ -63,7 +63,7 @@ public boolean verify(NKey nkey, byte[] input, byte[] signature) {
6363
}
6464
else {
6565
char[] encodedPublicKey = nkey.getPublicKey();
66-
byte[] decodedPublicKey = decode(nkey.getType(), encodedPublicKey);
66+
byte[] decodedPublicKey = nkeyDecode(nkey.getType(), encodedPublicKey);
6767
publicKey = new Ed25519PublicKeyParameters(decodedPublicKey);
6868
}
6969

core/src/test/java/io/nats/nkey/NKeyProviderTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static void beforeAll() {
3939
NKeyProvider.clearInstance();
4040
System.setProperty(NKEY_PROVIDER_CLASS_SYSTEM_PROPERTY, "io.nats.nkey.CoreNKeyProvider");
4141
PROVIDER = getProvider();
42+
PROVIDER = getProvider(); // this is coverage!
4243
}
4344

4445
@Test
@@ -47,7 +48,7 @@ public void testAccount() {
4748
assertNotNull(theKey);
4849

4950
char[] seed = theKey.getSeed();
50-
NKeyInternalUtils.decodeSeed(seed); // throws if there is an issue
51+
NKeyProviderUtils.decodeSeed(seed); // throws if there is an issue
5152

5253
assertEquals(PROVIDER.fromSeed(theKey.getSeed()), PROVIDER.fromSeed(theKey.getSeed()));
5354

@@ -81,7 +82,7 @@ public void testUser() {
8182
assertNotNull(theKey);
8283

8384
char[] seed = theKey.getSeed();
84-
NKeyInternalUtils.decodeSeed(seed); // throws if there is an issue
85+
NKeyProviderUtils.decodeSeed(seed); // throws if there is an issue
8586

8687
assertEquals(PROVIDER.fromSeed(theKey.getSeed()), PROVIDER.fromSeed(theKey.getSeed()));
8788

@@ -115,7 +116,7 @@ public void testCluster() {
115116
assertNotNull(theKey);
116117

117118
char[] seed = theKey.getSeed();
118-
NKeyInternalUtils.decodeSeed(seed); // throws if there is an issue
119+
NKeyProviderUtils.decodeSeed(seed); // throws if there is an issue
119120

120121
assertEquals(PROVIDER.fromSeed(theKey.getSeed()), PROVIDER.fromSeed(theKey.getSeed()));
121122

@@ -149,7 +150,7 @@ public void testOperator() {
149150
assertNotNull(theKey);
150151

151152
char[] seed = theKey.getSeed();
152-
NKeyInternalUtils.decodeSeed(seed); // throws if there is an issue
153+
NKeyProviderUtils.decodeSeed(seed); // throws if there is an issue
153154

154155
assertEquals(PROVIDER.fromSeed(theKey.getSeed()), PROVIDER.fromSeed(theKey.getSeed()));
155156

@@ -183,7 +184,7 @@ public void testServer() {
183184
assertNotNull(theKey);
184185

185186
char[] seed = theKey.getSeed();
186-
NKeyInternalUtils.decodeSeed(seed); // throws if there is an issue
187+
NKeyProviderUtils.decodeSeed(seed); // throws if there is an issue
187188

188189
assertEquals(PROVIDER.fromSeed(theKey.getSeed()), PROVIDER.fromSeed(theKey.getSeed()));
189190

@@ -406,8 +407,8 @@ public void testInterop() {
406407
assertArrayEquals(fromSeed.getPublicKey(), publicKey);
407408
assertArrayEquals(fromSeed.getPrivateKey(), privateKey);
408409

409-
NKeyDecodedSeed decoded = NKeyInternalUtils.decodeSeed(seed);
410-
char[] encodedSeed = NKeyInternalUtils.encodeSeed(NKeyType.fromPrefix(decoded.prefix), decoded.bytes);
410+
NKeyDecodedSeed decoded = NKeyProviderUtils.decodeSeed(seed);
411+
char[] encodedSeed = NKeyProviderUtils.encodeSeed(NKeyType.fromPrefix(decoded.prefix), decoded.bytes);
411412
assertArrayEquals(encodedSeed, seed);
412413
}
413414

0 commit comments

Comments
 (0)