|
| 1 | +// Copyright 2020-2024 The NATS Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at: |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package nats.io.nkeys; |
| 15 | + |
| 16 | +import net.i2p.crypto.eddsa.spec.EdDSANamedCurveSpec; |
| 17 | +import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable; |
| 18 | + |
| 19 | +import java.nio.ByteBuffer; |
| 20 | +import java.security.SecureRandom; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Random; |
| 23 | + |
| 24 | +public abstract class Common { |
| 25 | + // PrefixByteSeed is the prefix byte used for encoded NATS Seeds |
| 26 | + public static final int PREFIX_BYTE_SEED = 18 << 3; // Base32-encodes to 'S...' |
| 27 | + |
| 28 | + // PrefixBytePrivate is the prefix byte used for encoded NATS Private keys |
| 29 | + public static final int PREFIX_BYTE_PRIVATE = 15 << 3; // Base32-encodes to 'P...' |
| 30 | + |
| 31 | + // PrefixByteServer is the prefix byte used for encoded NATS Servers |
| 32 | + public static final int PREFIX_BYTE_SERVER = 13 << 3; // Base32-encodes to 'N...' |
| 33 | + |
| 34 | + // PrefixByteCluster is the prefix byte used for encoded NATS Clusters |
| 35 | + public static final int PREFIX_BYTE_CLUSTER = 2 << 3; // Base32-encodes to 'C...' |
| 36 | + |
| 37 | + // PrefixByteAccount is the prefix byte used for encoded NATS Accounts |
| 38 | + public static final int PREFIX_BYTE_ACCOUNT = 0; // Base32-encodes to 'A...' |
| 39 | + |
| 40 | + // PrefixByteUser is the prefix byte used for encoded NATS Users |
| 41 | + public static final int PREFIX_BYTE_USER = 20 << 3; // Base32-encodes to 'U...' |
| 42 | + |
| 43 | + // PrefixByteOperator is the prefix byte used for encoded NATS Operators |
| 44 | + public static final int PREFIX_BYTE_OPERATOR = 14 << 3; // Base32-encodes to 'O...' |
| 45 | + |
| 46 | + public static final int ED25519_PUBLIC_KEYSIZE = 32; |
| 47 | + public static final int ED25519_PRIVATE_KEYSIZE = 64; |
| 48 | + public static final int ED25519_SEED_SIZE = 32; |
| 49 | + |
| 50 | + // initialized in static initializer block |
| 51 | + public static final SecureRandom SRAND; |
| 52 | + public static final Random PRAND; |
| 53 | + |
| 54 | + public static final EdDSANamedCurveSpec ED_25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519); |
| 55 | + |
| 56 | + // XModem CRC based on the go version of NKeys |
| 57 | + public static final int[] CRC_16_TABLE = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, |
| 58 | + 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, |
| 59 | + 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, |
| 60 | + 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, |
| 61 | + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, |
| 62 | + 0xe7fe, 0xd79d, 0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, |
| 63 | + 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, |
| 64 | + 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, |
| 65 | + 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97, |
| 66 | + 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, |
| 67 | + 0x9f59, 0x8f78, 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, |
| 68 | + 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, |
| 69 | + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, |
| 70 | + 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, |
| 71 | + 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, |
| 72 | + 0x5634, 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, |
| 73 | + 0x18c0, 0x08e1, 0x3882, 0x28a3, 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, |
| 74 | + 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, |
| 75 | + 0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, |
| 76 | + 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 }; |
| 77 | + |
| 78 | + private static final String BASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; |
| 79 | + private static final int[] BASE32_LOOKUP = new int[256]; |
| 80 | + private static final int MASK = 31; |
| 81 | + private static final int SHIFT = 5; |
| 82 | + |
| 83 | + static { |
| 84 | + SRAND = new SecureRandom(); |
| 85 | + |
| 86 | + byte[] bytes = SRAND.generateSeed(8); |
| 87 | + ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE); |
| 88 | + buffer.put(bytes); |
| 89 | + buffer.flip();// need flip |
| 90 | + PRAND = new Random(buffer.getLong()); // seed with 8 bytes (64 bits) |
| 91 | + |
| 92 | + Arrays.fill(BASE32_LOOKUP, 0xFF); |
| 93 | + for (int i = 0; i < BASE32_CHARS.length(); i++) { |
| 94 | + int index = BASE32_CHARS.charAt(i) - '0'; |
| 95 | + BASE32_LOOKUP[index] = i; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static int crc16(byte[] bytes) { |
| 100 | + int crc = 0; |
| 101 | + |
| 102 | + for (byte b : bytes) { |
| 103 | + crc = ((crc << 8) & 0xffff) ^ CRC_16_TABLE[((crc >> 8) ^ (b & 0xFF)) & 0x00FF]; |
| 104 | + } |
| 105 | + |
| 106 | + return crc; |
| 107 | + } |
| 108 | + |
| 109 | + // http://en.wikipedia.org/wiki/Base_32 |
| 110 | + public static char[] base32Encode(final byte[] input) { |
| 111 | + int last = input.length; |
| 112 | + char[] charBuff = new char[(last + 7) * 8 / SHIFT]; |
| 113 | + int offset = 0; |
| 114 | + int buffer = input[offset++]; |
| 115 | + int bitsLeft = 8; |
| 116 | + int i = 0; |
| 117 | + |
| 118 | + while (bitsLeft > 0 || offset < last) { |
| 119 | + if (bitsLeft < SHIFT) { |
| 120 | + if (offset < last) { |
| 121 | + buffer <<= 8; |
| 122 | + buffer |= (input[offset++] & 0xff); |
| 123 | + bitsLeft += 8; |
| 124 | + } else { |
| 125 | + int pad = SHIFT - bitsLeft; |
| 126 | + buffer <<= pad; |
| 127 | + bitsLeft += pad; |
| 128 | + } |
| 129 | + } |
| 130 | + int index = MASK & (buffer >> (bitsLeft - SHIFT)); |
| 131 | + bitsLeft -= SHIFT; |
| 132 | + charBuff[i] = BASE32_CHARS.charAt(index); |
| 133 | + i++; |
| 134 | + } |
| 135 | + |
| 136 | + int nonBlank; |
| 137 | + |
| 138 | + for (nonBlank=charBuff.length-1;nonBlank>=0;nonBlank--) { |
| 139 | + if (charBuff[nonBlank] != 0) { |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + char[] retVal = new char[nonBlank+1]; |
| 145 | + |
| 146 | + System.arraycopy(charBuff, 0, retVal, 0, retVal.length); |
| 147 | + |
| 148 | + Arrays.fill(charBuff, '\0'); |
| 149 | + |
| 150 | + return retVal; |
| 151 | + } |
| 152 | + |
| 153 | + public static byte[] base32Decode(final char[] input) { |
| 154 | + byte[] bytes = new byte[input.length * SHIFT / 8]; |
| 155 | + int buffer = 0; |
| 156 | + int next = 0; |
| 157 | + int bitsLeft = 0; |
| 158 | + |
| 159 | + for (char value : input) { |
| 160 | + int lookup = value - '0'; |
| 161 | + |
| 162 | + if (lookup < 0 || lookup >= BASE32_LOOKUP.length) { |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + int c = BASE32_LOOKUP[lookup]; |
| 167 | + buffer <<= SHIFT; |
| 168 | + buffer |= c & MASK; |
| 169 | + bitsLeft += SHIFT; |
| 170 | + if (bitsLeft >= 8) { |
| 171 | + bytes[next++] = (byte) (buffer >> (bitsLeft - 8)); |
| 172 | + bitsLeft -= 8; |
| 173 | + } |
| 174 | + } |
| 175 | + return bytes; |
| 176 | + } |
| 177 | +} |
0 commit comments