private String signHmacSha1(String value, String key) { try { // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes(StandardCharsets.UTF_8)); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Convert array of Hex bytes to a String return new String(hexBytes, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void testEncodeHexByteBufferEmpty() { assertTrue(Arrays.equals(new char[0], Hex.encodeHex(ByteBuffer.allocate(0)))); assertTrue(Arrays.equals(new byte[0], new Hex().encode(ByteBuffer.allocate(0)))); }
final byte[] actualEncodedBytes = customCodec.encode(sourceBytes);
@Test public void testEncodeHexByteArrayEmpty() { assertTrue(Arrays.equals(new char[0], Hex.encodeHex(new byte[0]))); assertTrue(Arrays.equals(new byte[0], new Hex().encode(new byte[0]))); }
@Test public void testEncodeByteBufferEmpty() { assertTrue(Arrays.equals(new byte[0], new Hex().encode(ByteBuffer.allocate(0)))); }
@Test public void testEncodeDecodeHexCharArrayRandom() throws DecoderException, EncoderException { final Random random = new Random(); final Hex hex = new Hex(); for (int i = 5; i > 0; i--) { final byte[] data = new byte[random.nextInt(10000) + 1]; random.nextBytes(data); // static API final char[] encodedChars = Hex.encodeHex(data); byte[] decodedBytes = Hex.decodeHex(encodedChars); assertTrue(Arrays.equals(data, decodedBytes)); // instance API with array parameter final byte[] encodedStringBytes = hex.encode(data); decodedBytes = hex.decode(encodedStringBytes); assertTrue(Arrays.equals(data, decodedBytes)); // instance API with char[] (Object) parameter String dataString = new String(encodedChars); char[] encodedStringChars = (char[]) hex.encode(dataString); decodedBytes = (byte[]) hex.decode(encodedStringChars); assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), decodedBytes)); // instance API with String (Object) parameter dataString = new String(encodedChars); encodedStringChars = (char[]) hex.encode(dataString); decodedBytes = (byte[]) hex.decode(new String(encodedStringChars)); assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), decodedBytes)); } }
@Test public void testEncodeByteBufferObjectEmpty() throws EncoderException { assertTrue(Arrays.equals(new char[0], (char[]) new Hex().encode((Object) ByteBuffer.allocate(0)))); }
@Test public void testEncodeStringEmpty() throws EncoderException { assertTrue(Arrays.equals(new char[0], (char[]) new Hex().encode(""))); }
@Test public void testEncodeByteArrayEmpty() { assertTrue(Arrays.equals(new byte[0], new Hex().encode(new byte[0]))); }
@Test public void testEncodeByteArrayObjectEmpty() throws EncoderException { assertTrue(Arrays.equals(new char[0], (char[]) new Hex().encode((Object) new byte[0]))); }
if(occurred == Occurred.BEFORE) { throw new ObsoleteVersionException("Key " + new String(hexCodec.encode(key.get())) + " " + value.getVersion().toString()
@Test public void testEncodeClassCastException() { try { new Hex().encode(new int[] { 65 }); fail("An exception wasn't thrown when trying to encode."); } catch (final EncoderException e) { // Expected exception } }
if(occurred == Occurred.BEFORE) throw new ObsoleteVersionException("Key " + new String(hexCodec.encode(key.get())) + " " + value.getVersion().toString()
@Override public byte[] mutate(byte[] currentValue) throws Exception { String cvs = new String(currentValue, UTF_8); String[] tokens = cvs.split(","); long flushID = Long.parseLong(tokens[0]); flushID++; String txidString = String.format("%016x", tid); for (int i = 1; i < tokens.length; i++) { if (tokens[i].startsWith(txidString)) continue; // skip self log.debug("txidString : {}", txidString); log.debug("tokens[{}] : {}", i, tokens[i]); throw new AcceptableThriftTableOperationException(tableId.canonicalID(), null, TableOperation.COMPACT, TableOperationExceptionType.OTHER, "Another compaction with iterators and/or a compaction strategy is running"); } StringBuilder encodedIterators = new StringBuilder(); if (config != null) { Hex hex = new Hex(); encodedIterators.append(","); encodedIterators.append(txidString); encodedIterators.append("="); encodedIterators.append(new String(hex.encode(config), UTF_8)); } return (Long.toString(flushID) + encodedIterators).getBytes(UTF_8); } });
private String expectedHexString(MessageDigest expected) { return new String(HEX.encode(expected.digest()), StandardCharsets.UTF_8); } }
private TransactionId(final PublicKey pubKey, final String digestAlgorithm) { MessageDigest digest; try { digest = MessageDigest.getInstance(digestAlgorithm); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } id = new Hex().encode(digest.digest(pubKey.getEncoded())); }
/** * 获取十六进制字符串形式的MD5摘要 */ private static String md5Hex(String src) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] bs = md5.digest(src.getBytes()); return new String(new Hex().encode(bs)); } catch (Exception e) { return null; } }
/** * Convert a hex byte array back to a String * * @param bytes The hex byte array * @return The string */ public static final String byteArrayToHexString(byte[] bytes) { return new String(new Hex().encode(bytes), Charset.forName("UTF-8")); }
/** * Converts a byte array to a hex string. * * @param bytes The input byte array. * @return The output hex string. */ public static String getString(final byte[] bytes) { final Hex codec = new Hex(); final byte[] decodedBytes = codec.encode(bytes); return StringEncoder.getString(decodedBytes); } }
private String saltAndEncrypt(char[] data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { String value; Hex hex = new Hex(); char[] saltedData = addSalt(data); byte[] encode = encrypt(saltedData, password); value = new String(hex.encode(encode)); return value; }