/** * Creates a new instance. * * @param fixed User-defined fixed field value. * @param invocations Initial invocation count. The invocations field is incremented _before_ use in {@link * #generate()}. */ public CounterNonce(final String fixed, final long invocations) { this(ByteUtil.toBytes(fixed), invocations); }
/** * Converts a byte buffer into a string in the UTF-8 character set. * * @param buffer Byte buffer to convert. * * @return UTF-8 string representation of bytes. */ public static String toString(final ByteBuffer buffer) { return toCharBuffer(buffer).toString(); }
/** * Decodes the given encoded data using the given char-to-byte decoder. * * @param decoder Decoder to perform char-to-byte conversion. * @param encoded Encoded character data. * * @return Decoded data as raw bytes. * * @throws EncodingException on decoding errors. */ public static byte[] decode(final Decoder decoder, final CharSequence encoded) throws EncodingException { final ByteBuffer output = ByteBuffer.allocate(decoder.outputSize(encoded.length())); decoder.decode(CharBuffer.wrap(encoded), output); decoder.finalize(output); output.flip(); return ByteUtil.toArray(output); } }
final int length = ByteUtil.readInt(input); if (length < 0) { throw new EncodingException("Invalid ciphertext header length: " + length); int nonceLen = 0; try { nonceLen = ByteUtil.readInt(input); nonce = new byte[nonceLen]; input.read(nonce); int keyLen = 0; try { keyLen = ByteUtil.readInt(input); b = new byte[keyLen]; input.read(b);
return ByteUtil.toArray(output);
/** * Creates a new instance. * * @param fixed User-defined fixed field value. * @param invocations Initial invocation count. The invocations field is incremented _before_ use in {@link * #generate()}. */ public CounterNonce(final long fixed, final long invocations) { this(ByteUtil.toBytes(fixed), invocations); }
/** * Decodes an input string into a byte array using the configured decoder. * * @param input Input string to decode. * @param length Desired output size in bytes. * * @return Input decoded as a byte array. */ private static byte[] decode(final String input, final int length) { final Decoder decoder = new Base64Decoder.Builder().setAlphabet(ALPHABET).setPadding(false).build(); final ByteBuffer output = ByteBuffer.allocate(decoder.outputSize(input.length())); decoder.decode(CharBuffer.wrap(input), output); decoder.finalize(output); output.flip(); if (output.limit() != length) { throw new IllegalArgumentException("Input is not of the expected size: " + output.limit() + "!=" + length); } return ByteUtil.toArray(output); }
/** * Converts an integer into a 4-byte big endian array. * * @param value Integer value to convert. * * @return 4-byte big-endian representation of integer value. */ public static byte[] toBytes(final int value) { final byte[] bytes = new byte[4]; toBytes(value, bytes, 0); return bytes; }
/** * Creates a new instance. Instances of this method produces nonces of the default length, {@value #DEFAULT_LENGTH}, * and are not subject to constraints on the number of invocations. * * @param fixed User-defined fixed field value. * @param invocations Initial invocation count. The invocations field is incremented _before_ use in {@link * #generate()}. */ public CounterNonce(final int fixed, final long invocations) { this(ByteUtil.toBytes(fixed), invocations); }
/** * Converts a long integer into an 8-byte big endian array. * * @param value Long integer value to convert. * * @return 8-byte big-endian representation of long integer value. */ public static byte[] toBytes(final long value) { final byte[] bytes = new byte[8]; toBytes(value, bytes, 0); return bytes; }
@Override public byte[] generate() throws LimitException { return ByteUtil.toBytes(counter.incrementAndGet()); }
/** * Sets the PEM-encoded public key data. * * @param pemEncodedKey PEM-encoded public key data. */ public void setEncodedKey(final String pemEncodedKey) { if (!PemUtil.isPem(ByteUtil.toBytes(pemEncodedKey))) { throw new IllegalArgumentException("Data is not PEM encoded."); } this.encodedKey = pemEncodedKey; }
/** * Sets the PEM-encoded private key data. * * @param pemEncodedKey PEM-encoded private key data. */ public void setEncodedKey(final String pemEncodedKey) { if (!PemUtil.isPem(ByteUtil.toBytes(pemEncodedKey))) { throw new IllegalArgumentException("Data is not PEM encoded."); } this.encodedKey = pemEncodedKey; }
/** * Creates a new instance using the given fixed field value. * * @param fixed User-defined fixed field value. * @param randomLength Number of bytes in the random part of the nonce. MUST be at least 12. */ public RBGNonce(final String fixed, final int randomLength) { if (randomLength < 12) { throw new IllegalArgumentException("Must specify at least 12 bytes (96 bits) for random part."); } this.randomLength = randomLength; if (fixed != null) { this.fixed = ByteUtil.toBytes(fixed); } else { this.fixed = new byte[0]; } this.rbg = newRBG(this.randomLength, this.fixed); }
@Override public byte[] generate() throws LimitException { final byte[] value = new byte[getLength()]; System.arraycopy(fixed, 0, value, 0, fixed.length); final long next = count.incrementAndGet(); if (value.length != DEFAULT_LENGTH) { // Enforce constraints described in section 8.3 if (next > MAX_INVOCATIONS) { throw new LimitException("Exceeded 2^32 invocations."); } } ByteUtil.toBytes(next, value, fixed.length); return value; }
@Override public CachedData encode(final MemcachedStorageRecord o) { final byte[] value = o.getValue().getBytes(StandardCharsets.UTF_8); final byte[] encoded = new byte[value.length + 8]; ByteUtil.toBytes(o.getExpiration() == null ? 0 : o.getExpiration().longValue(), encoded, 0); System.arraycopy(value, 0, encoded, 8, value.length); return new CachedData(0, encoded, MAX_SIZE); }
/** * Creates a cache-wide unique namespace for the given context name. The context-namespace mapping is stored * in the cache. * * @param context Context name. * * @return Namespace name for given context. * * @throws java.io.IOException On memcached operation errors. */ protected String createNamespace(final String context) throws IOException { String namespace = null; boolean success = false; // Perform successive add operations until success to ensure unique namespace while (!success) { namespace = CodecUtil.hex(ByteUtil.toBytes(System.currentTimeMillis())); // Namespace values are safe for memcached keys success = handleAsyncResult(memcacheClient.add(namespace, 0, context, stringTranscoder)); } // Create the reverse mapping to support looking up namespace by context name if (!handleAsyncResult(memcacheClient.add(memcachedKey(context), 0, namespace, stringTranscoder))) { throw new IllegalStateException(context + " already exists"); } return namespace; }
/** * Internal OTP generation method. * * @param key Per-user key. * @param count Counter moving factor. * * @return Integer OTP. */ protected int generateInternal(final byte[] key, final long count) { final HMac hmac = new HMac(getDigest()); final byte[] output = new byte[hmac.getMacSize()]; hmac.init(new KeyParameter(key)); hmac.update(ByteUtil.toBytes(count), 0, 8); hmac.doFinal(output, 0); return truncate(output) % MODULUS[numberOfDigits]; }
digest.update(bytes, 0, bytes.length); } else if (o instanceof String) { final byte[] bytes = ByteUtil.toBytes((String) o); digest.update(bytes, 0, bytes.length); } else if (o instanceof InputStream) {