public void calculateHMAC_SHA256 ( byte [ ] message , int messageOffset , int messageLen , byte [ ] key , int keyLen , byte [ ] digest , int digestOffset ) { if ( log . isTraceEnabled ( ) ) { log . trace ( "calculateHMAC_SHA256 - messageOffset: {} messageLen: {}" , messageOffset , messageLen ) ; log . trace ( "calculateHMAC_SHA256 - message: {}" , Hex . encodeHexString ( Arrays . copyOfRange ( message , messageOffset , messageOffset + messageLen ) ) ) ; log . trace ( "calculateHMAC_SHA256 - keyLen: {} key: {}" , keyLen , Hex . encodeHexString ( Arrays . copyOf ( key , keyLen ) ) ) ; //log.trace("calculateHMAC_SHA256 - digestOffset: {} digest: {}", digestOffset, Hex.encodeHexString(Arrays.copyOfRange(digest, digestOffset, digestOffset + DIGEST_LENGTH))); } byte [ ] calcDigest ; try { Mac hmac = Mac . getInstance ( "Hmac-SHA256" , BouncyCastleProvider . PROVIDER_NAME ) ; hmac . init ( new SecretKeySpec ( Arrays . copyOf ( key , keyLen ) , "HmacSHA256" ) ) ; byte [ ] actualMessage = Arrays . copyOfRange ( message , messageOffset , messageOffset + messageLen ) ; calcDigest = hmac . doFinal ( actualMessage ) ; //if (log.isTraceEnabled()) { // log.trace("Calculated digest: {}", Hex.encodeHexString(calcDigest)); //} System . arraycopy ( calcDigest , 0 , digest , digestOffset , DIGEST_LENGTH ) ; } catch ( InvalidKeyException e ) { log . error ( "Invalid key" , e ) ; } catch ( Exception e ) { log . error ( "Hash calculation failed" , e ) ; } }
Calculates an HMAC SHA256 hash into the digest at the given offset .