/** * Converts a String representing hexadecimal values into an array of bytes of those same values. The * returned array will be half the length of the passed String, as it takes two characters to represent any given * byte. An exception is thrown if the passed String has an odd number of elements. * * @param data * A String containing hexadecimal digits * @return A byte array containing binary data decoded from the supplied char array. * @throws DecoderException * Thrown if an odd number or illegal of characters is supplied * @since 1.11 */ public static byte[] decodeHex(final String data) throws DecoderException { return decodeHex(data.toCharArray()); }
public static byte[] parseHex(String string) { try { return Hex.decodeHex(string); } catch (DecoderException e) { throw new RuntimeException(e); } }
/** * Converts a buffer of character bytes representing hexadecimal values into an array of bytes of those same values. * The returned array will be half the length of the passed array, as it takes two characters to represent any given * byte. An exception is thrown if the passed char array has an odd number of elements. * * @param buffer * An array of character bytes containing hexadecimal digits * @return A byte array containing binary data decoded from the supplied byte array (representing characters). * @throws DecoderException * Thrown if an odd number of characters is supplied to this function * @see #decodeHex(char[]) * @since 1.11 */ public byte[] decode(final ByteBuffer buffer) throws DecoderException { return decodeHex(new String(buffer.array(), getCharset()).toCharArray()); }
/** * Converts an array of character bytes representing hexadecimal values into an array of bytes of those same values. * The returned array will be half the length of the passed array, as it takes two characters to represent any given * byte. An exception is thrown if the passed char array has an odd number of elements. * * @param array * An array of character bytes containing hexadecimal digits * @return A byte array containing binary data decoded from the supplied byte array (representing characters). * @throws DecoderException * Thrown if an odd number of characters is supplied to this function * @see #decodeHex(char[]) */ @Override public byte[] decode(final byte[] array) throws DecoderException { return decodeHex(new String(array, getCharset()).toCharArray()); }
public byte[] getCheckSum() throws DecoderException { String checkSumString = (String) get(ReadOnlyStorageMetadata.CHECKSUM); if(checkSumString == null) { return null; } return Hex.decodeHex(checkSumString.toCharArray()); }
public byte[] getCheckSum() throws DecoderException { String checkSumString = (String) get(ReadOnlyStorageMetadata.CHECKSUM); if(checkSumString == null) { return null; } return Hex.decodeHex(checkSumString.toCharArray()); }
@Override public void writeSchemaVersioningInformation(Schema schema, DataOutputStream outputStream) throws IOException { String schemaId = this.schemaId.isPresent() ? this.schemaId.get() : this.getIdForSchema(schema); outputStream.writeByte(KafkaAvroSchemaRegistry.MAGIC_BYTE); try { outputStream.write(Hex.decodeHex(schemaId.toCharArray())); } catch (DecoderException exception) { throw new IOException(exception); } }
private static SecretKey getMasterKey() throws KeyManagementException { try { // Get the master encryption key from bootstrap.conf String masterKeyHex = NiFiPropertiesLoader.extractKeyFromBootstrapFile(); return new SecretKeySpec(Hex.decodeHex(masterKeyHex.toCharArray()), "AES"); } catch (IOException | DecoderException e) { logger.error("Encountered an error: ", e); throw new KeyManagementException(e); } } }
public BlowfishTupleSerializer(Kryo kryo, Map storm_conf) { String encryption_key = null; try { encryption_key = (String) storm_conf.get(SECRET_KEY); LOG.debug("Blowfish serializer being constructed ..."); if (encryption_key == null) { throw new RuntimeException("Blowfish encryption key not specified"); } byte[] bytes = Hex.decodeHex(encryption_key.toCharArray()); _serializer = new BlowfishSerializer(new ListDelegateSerializer(), bytes); } catch (org.apache.commons.codec.DecoderException ex) { throw new RuntimeException("Blowfish encryption key invalid", ex); } }
public void setSecretKey( String keyString ) throws CryptoKeyException { try { setSecretKey( Hex.decodeHex( keyString.toCharArray() ) ); } catch ( Exception e ) { throw new CryptoKeyException( e ); } }
private boolean isValidSignature(String prefix, String suffix, Mac mac) { try { byte[] ourSuffix = Util.truncate(getHmac(key, prefix.getBytes(), mac), 10); byte[] theirSuffix = Hex.decodeHex(suffix.toCharArray()); return MessageDigest.isEqual(ourSuffix, theirSuffix); } catch (DecoderException e) { logger.warn("DirectoryCredentials", e); return false; } }
private void checkDecodeHexCharArrayOddCharacters(final String data) { try { Hex.decodeHex(data); fail("An exception wasn't thrown when trying to decode an odd number of characters"); } catch (final DecoderException e) { // Expected exception } }
private void checkDecodeHexCharArrayOddCharacters(final char[] data) { try { Hex.decodeHex(data); fail("An exception wasn't thrown when trying to decode an odd number of characters"); } catch (final DecoderException e) { // Expected exception } }
@Test public void shouldReEncryptUsingNewKey() throws DecoderException, CryptoException { String originalCipherText = "mvcX9yrQsM4iPgm1tDxN1A=="; String newCipherText = DESEncrypter.reEncryptUsingNewKey(decodeHex("269298bc31c44620"), decodeHex("02644c13cb892962"), originalCipherText); assertThat(originalCipherText).isNotEqualTo(newCipherText); DESCipherProvider newCipher = mock(DESCipherProvider.class); when(newCipher.getKey()).thenReturn(decodeHex("02644c13cb892962")); DESEncrypter newEncrypter = new DESEncrypter(newCipher); assertThat(newEncrypter.decrypt(newCipherText)).isEqualTo("user-password!"); } }
@Test public void testDecodeHexCharArrayEmpty() throws DecoderException { assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(new char[0]))); }
@Test public void testDecodeHexStringEmpty() throws DecoderException { assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(""))); }
@Before public void setUp() throws Exception { AESCipherProvider cipherProvider = mock(AESCipherProvider.class); when(cipherProvider.getKey()).thenReturn(decodeHex("fdf500c4ec6e51172477145db6be63c5")); aesEncrypter = new AESEncrypter(cipherProvider); }
@Before public void setUp() throws Exception { DESCipherProvider cipherProvider = mock(DESCipherProvider.class); when(cipherProvider.getKey()).thenReturn(decodeHex("269298bc31c44620")); desEncrypter = new DESEncrypter(cipherProvider); }
@Override public ValidationResult validate(final String subject, final String input, final ValidationContext validationContext) { try { Hex.decodeHex(input.toCharArray()); return new ValidationResult.Builder().valid(true).input(input).subject(subject).build(); } catch (final Exception e) { return new ValidationResult.Builder().valid(false).explanation("Not a valid Hex String").input(input).subject(subject).build(); } } }
@Test public void testByteToStringVariations() throws DecoderException { final Base64 base64 = new Base64(0); final byte[] b1 = StringUtils.getBytesUtf8("Hello World"); final byte[] b2 = new byte[0]; final byte[] b3 = null; final byte[] b4 = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090"); // for // url-safe // tests assertEquals("byteToString Hello World", "SGVsbG8gV29ybGQ=", base64.encodeToString(b1)); assertEquals("byteToString static Hello World", "SGVsbG8gV29ybGQ=", Base64.encodeBase64String(b1)); assertEquals("byteToString \"\"", "", base64.encodeToString(b2)); assertEquals("byteToString static \"\"", "", Base64.encodeBase64String(b2)); assertEquals("byteToString null", null, base64.encodeToString(b3)); assertEquals("byteToString static null", null, Base64.encodeBase64String(b3)); assertEquals("byteToString UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", base64.encodeToString(b4)); assertEquals("byteToString static UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", Base64.encodeBase64String(b4)); assertEquals("byteToString static-url-safe UUID", "K_fMJwH-Q5e0nr7tWsxwkA", Base64.encodeBase64URLSafeString(b4)); }