Example usage for org.apache.commons.codec.digest HmacUtils hmacSha512

List of usage examples for org.apache.commons.codec.digest HmacUtils hmacSha512

Introduction

In this page you can find the example usage for org.apache.commons.codec.digest HmacUtils hmacSha512.

Prototype

public static byte[] hmacSha512(final String key, final String valueToDigest) 

Source Link

Document

Returns a HmacSHA512 Message Authentication Code (MAC) for the given key and value.

Usage

From source file:com.vmware.o11n.plugin.crypto.service.CryptoDigestService.java

/**
 * HmacSHA512/*from   www.  j av  a 2 s  .  c o m*/
 *
 * @param keyB64 Secret key Base64 encoded
 * @param dataB64 Data to sign Base64 encoded
 * @return HmacSha512 MAC for the given key and data Base64 encoded
 */
public String hmacSha512(String keyB64, String dataB64) {
    validateB64(keyB64);
    validateB64(dataB64);
    final byte[] key = Base64.decodeBase64(keyB64);
    final byte[] data = Base64.decodeBase64(dataB64);

    return Base64.encodeBase64String(HmacUtils.hmacSha512(key, data));
}

From source file:se.sawano.java.security.otp.rfc6238.ExploratoryTests.java

@Test
public void should_perform_TOTP() throws Exception {
    System.out.println("--> " + hexStr2Bytes(seed).length);

    final ReferenceDataRepository testData = new ReferenceDataRepository().init();

    final ReferenceDataRepository.ReferenceData data = testData
            .getForMode(ReferenceDataRepository.ReferenceData.Mode.SHA512);

    assertArrayEquals(hexStr2Bytes(seed64), secret().value());

    final int numberOfDigitsInCode = 8;
    final long T0 = ReferenceDataRepository.T0.toEpochMilli();
    final long stepSize = ReferenceDataRepository.TIME_STEP.toMillis();
    final long T = (data.time.toEpochMilli() - T0) / stepSize; // Number of steps

    final String hexT = StringUtils.leftPad(Long.toHexString(T), 16, '0');

    System.out.println("Thex=" + hexT);
    assertEquals(data.hexTime, hexT);/*from   w  ww  . java2s . com*/

    final byte[] hexTBytes = Hex.decodeHex(hexT.toCharArray());
    final byte[] hextBytes2 = hexStr2Bytes(hexT);
    assertArrayEquals(hextBytes2, hexTBytes);

    final byte[] hashBytes = HmacUtils.hmacSha512(secret().value(), hexTBytes);
    System.out.println(hashBytes.length);

    final int binary = truncate(hashBytes);

    int otp = binary % DIGITS_POWER[numberOfDigitsInCode];

    final String totpString = StringUtils.leftPad(Integer.toString(otp), numberOfDigitsInCode, '0');
    System.out.println(totpString);
    assertEquals(data.totp, totpString);
}