2323
2424import 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 ;
0 commit comments