List of usage examples for org.bouncycastle.crypto.macs HMac reset
public void reset()
From source file:com.entertailion.android.shapeways.api.Request.java
License:Apache License
/** * Generate HMAC-SHA1 for message/*from w ww. ja v a 2 s. co m*/ * * @param key * @param message * @return * @throws Exception */ private static String generateHmac(String key, String message) throws Exception { Log.d(LOG_TAG, "generateHmac: " + key + "=" + message); byte[] keyBytes = key.getBytes(ShapewaysClient.ENCODING); byte[] data = message.getBytes(ShapewaysClient.ENCODING); HMac macProvider = new HMac(new SHA1Digest()); macProvider.init(new KeyParameter(keyBytes)); macProvider.reset(); macProvider.update(data, 0, data.length); byte[] output = new byte[macProvider.getMacSize()]; macProvider.doFinal(output, 0); byte[] hmac = Base64.encode(output); return new String(hmac).replaceAll("\r\n", ""); }
From source file:com.google.bitcoin.crypto.HDUtils.java
License:Apache License
static byte[] hmacSha512(HMac hmacSha512, byte[] input) { hmacSha512.reset(); hmacSha512.update(input, 0, input.length); byte[] out = new byte[64]; hmacSha512.doFinal(out, 0);//from ww w. ja v a 2 s .c om return out; }
From source file:COSE.Recipient.java
private byte[] HKDF(byte[] secret, int cbitKey, AlgorithmID algorithmID, Digest digest) { byte[] rgbContext = GetKDFInput(cbitKey, algorithmID); CBORObject obj = findAttribute(HeaderKeys.HKDF_Salt.AsCBOR()); // Perform the Extract phase HMac mac = new HMac(digest); int hashLength = digest.getDigestSize(); int c = ((cbitKey + 7) / 8 + hashLength - 1) / hashLength; byte[] K = new byte[digest.getDigestSize()]; if (obj != null) K = obj.GetByteString();/*from www . ja v a 2 s.co m*/ KeyParameter key = new KeyParameter(K); mac.init(key); mac.update(secret, 0, secret.length); byte[] rgbExtract = new byte[hashLength]; mac.doFinal(rgbExtract, 0); // Now do the Expand phase byte[] rgbOut = new byte[cbitKey / 8]; byte[] rgbT = new byte[hashLength * c]; mac = new HMac(digest); key = new KeyParameter(rgbExtract); mac.init(key); byte[] rgbLast = new byte[0]; byte[] rgbHash2 = new byte[hashLength]; for (int i = 0; i < c; i++) { mac.reset(); mac.update(rgbLast, 0, rgbLast.length); mac.update(rgbContext, 0, rgbContext.length); mac.update((byte) (i + 1)); rgbLast = rgbHash2; mac.doFinal(rgbLast, 0); System.arraycopy(rgbLast, 0, rgbT, i * hashLength, hashLength); } System.arraycopy(rgbT, 0, rgbOut, 0, cbitKey / 8); return rgbOut; }
From source file:org.crypto.sse.CryptoPrimitives.java
License:Open Source License
public static byte[] generateHmac(byte[] key, String msg) throws UnsupportedEncodingException { HMac hmac = new HMac(new SHA256Digest()); byte[] result = new byte[hmac.getMacSize()]; byte[] msgAry = msg.getBytes("UTF-8"); hmac.init(new KeyParameter(key)); hmac.reset(); hmac.update(msgAry, 0, msgAry.length); hmac.doFinal(result, 0);/*from ww w.j av a 2 s. c o m*/ return result; }
From source file:org.crypto.sse.CryptoPrimitives.java
License:Open Source License
public static byte[] generateHmac(byte[] key, byte[] msg) throws UnsupportedEncodingException { HMac hmac = new HMac(new SHA256Digest()); byte[] result = new byte[hmac.getMacSize()]; hmac.init(new KeyParameter(key)); hmac.reset(); hmac.update(msg, 0, msg.length);/*from w w w. j a v a2 s. c om*/ hmac.doFinal(result, 0); return result; }
From source file:org.crypto.sse.CryptoPrimitives.java
License:Open Source License
public static byte[] generateHmac512(byte[] key, String msg) throws UnsupportedEncodingException { HMac hmac = new HMac(new SHA512Digest()); byte[] result = new byte[hmac.getMacSize()]; byte[] msgAry = msg.getBytes("UTF-8"); hmac.init(new KeyParameter(key)); hmac.reset(); hmac.update(msgAry, 0, msgAry.length); hmac.doFinal(result, 0);//from w w w . j a va 2 s. c o m return result; }
From source file:org.panbox.core.crypto.io.AuthTagVerifierTest.java
License:Open Source License
/** * Test method for// ww w. ja v a 2s.c om * {@link org.panbox.core.crypto.io.AuthTagVerifier#updateFileAuthTag()}. * * @throws FileEncryptionException */ @Test public void testUpdateFileAuthTag() throws Exception { byte[] testTag = new byte[TAGLEN]; byte[] referenceFileTag = new byte[testVerifier.authTagHMac.getMacSize()]; HMac refHMac = testVerifier.authTagHMac.getClass().getConstructor(Digest.class) .newInstance(new SHA256Digest()); refHMac.init(new KeyParameter(testKey.getEncoded())); // exception should be thrown if AuTagVerifier-HMac cannot be properly // initialized aesTestFile.shareKey = null; try { testVerifier.updateFileAuthTag(); fail("Expected exception for invalid file encryption key!"); } catch (FileEncryptionException e) { assertEquals("Invalid file encryption key in encrypting random access file!", e.getMessage()); } // restore key aesTestFile.shareKey = testKey; // test if exception is thrown upon file auth tag update without chunk // auth tags try { testVerifier.updateFileAuthTag(); fail("Expected exception as no chunk auth tags have been set!"); } catch (FileEncryptionException e) { assertEquals("No chunk authentication tags have been set yet!", e.getMessage()); } Arrays.fill(testTag, (byte) 0x41); refHMac.update(testTag, 0, TAGLEN); testVerifier.insertChunkAuthTag(0, testTag); // skip one chunk Arrays.fill(testTag, (byte) 0x42); refHMac.update(testTag, 0, TAGLEN); testVerifier.insertChunkAuthTag(2, testTag); try { testVerifier.updateFileAuthTag(); fail("Missing authentication tag should cause an exception"); } catch (FileEncryptionException e) { assertEquals("Invalid chunk authentication tag in auth tag table at offset: 1", e.getMessage()); } Arrays.fill(testTag, (byte) 0x43); refHMac.update(testTag, 0, TAGLEN); testVerifier.insertChunkAuthTag(1, testTag); assertNull(aesTestFile.readFileAuthenticationTag()); refHMac.doFinal(referenceFileTag, 0); testVerifier.updateFileAuthTag(); assertNotNull(aesTestFile.readFileAuthenticationTag()); // should NOT be equal as update order for refHMac was 0 - 2 - 1 assertFalse(Arrays.equals(referenceFileTag, aesTestFile.readFileAuthenticationTag())); // calculate actual value refHMac.reset(); Arrays.fill(testTag, (byte) 0x41); refHMac.update(testTag, 0, TAGLEN); Arrays.fill(testTag, (byte) 0x43); refHMac.update(testTag, 0, TAGLEN); Arrays.fill(testTag, (byte) 0x42); refHMac.update(testTag, 0, TAGLEN); refHMac.doFinal(referenceFileTag, 0); assertArrayEquals(referenceFileTag, aesTestFile.readFileAuthenticationTag()); }