List of usage examples for org.bouncycastle.crypto.macs HMac init
public void init(CipherParameters params)
From source file:com.DSC.crypto.Cipher.java
License:Open Source License
/** * //from w ww .jav a2 s . co m * @param passphrase * @param data * @return */ public static BigInteger[] generateHMAC(String passphrase, byte[] data) { HMac hmac = new HMac(new MD5Digest()); byte[] buf = new byte[hmac.getMacSize()]; BigInteger[] hmacBigInt = new BigInteger[1]; /* Initializes and generate HMAC for message */ hmac.init(new KeyParameter(passphrase.getBytes())); hmac.update(data, 0, data.length); hmac.doFinal(buf, 0); /* Convert the HMAC to a big integer representation */ hmacBigInt[0] = new BigInteger(buf); return hmacBigInt; }
From source file:com.entertailion.android.shapeways.api.Request.java
License:Apache License
/** * Generate HMAC-SHA1 for message/* w ww . j ava 2 s . c o 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 HMac createHmacSha512Digest(byte[] key) { SHA512Digest digest = new SHA512Digest(); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(key)); return hMac;// w ww. j a va 2s .com }
From source file:com.hanhuy.keepassj.HmacOtp.java
License:Open Source License
public static String Generate(byte[] pbSecret, long uFactor, int uCodeDigits, boolean bAddChecksum, int iTruncationOffset) { byte[] pbText = MemUtil.UInt64ToBytes(uFactor); StrUtil.ArraysReverse(pbText); // Big-Endian HMac hsha1 = new HMac(new SHA1Digest()); KeyParameter key = new KeyParameter(pbSecret); hsha1.init(key); byte[] pbHash = new byte[hsha1.getMacSize()]; hsha1.update(pbText, 0, pbText.length); hsha1.doFinal(pbHash, 0);//from w ww .j a v a 2 s. c om int uOffset = (int) (pbHash[pbHash.length - 1] & 0xF); if ((iTruncationOffset >= 0) && (iTruncationOffset < (pbHash.length - 4))) uOffset = (int) iTruncationOffset; int uBinary = (int) (((pbHash[uOffset] & 0x7F) << 24) | ((pbHash[uOffset + 1] & 0xFF) << 16) | ((pbHash[uOffset + 2] & 0xFF) << 8) | (pbHash[uOffset + 3] & 0xFF)); int uOtp = (uBinary % vDigitsPower[uCodeDigits]); if (bAddChecksum) uOtp = ((uOtp * 10) + CalculateChecksum(uOtp, uCodeDigits)); int uDigits = (bAddChecksum ? (uCodeDigits + 1) : uCodeDigits); return String.format("%0" + uDigits + "d", uOtp); }
From source file:com.joyent.manta.client.crypto.EncryptingEntityHelper.java
License:Open Source License
/** * Creates a new {@link OutputStream} implementation that is backed directly * by a {@link CipherOutputStream} or a {@link HmacOutputStream} that wraps * a {@link CipherOutputStream} depending on the encryption cipher/mode being * used. This allows us to support EtM authentication for ciphers/modes that * do not natively support authenticating encryption. * * NOTE: The design of com.joyent.manta.client.multipart.EncryptionStateRecorder * is heavily coupled to this implementation! Changing how these streams are * wrapped requires changes to EncryptionStateRecorder! * * @param httpOut output stream for writing to the HTTP network socket * @param encryptionContext current encryption running state * @return a new stream configured based on the parameters *///from w w w .j a v a 2 s .c o m public static OutputStream makeCipherOutputForStream(final OutputStream httpOut, final EncryptionContext encryptionContext) { final HMac hmac; if (encryptionContext.getCipherDetails().isAEADCipher()) { hmac = null; } else { hmac = encryptionContext.getCipherDetails().getAuthenticationHmac(); Validate.notNull(encryptionContext.getSecretKey(), "Secret key must not be null"); hmac.init(new KeyParameter(encryptionContext.getSecretKey().getEncoded())); /* The first bytes of the HMAC are the IV. This is done in order to * prevent IV collision or spoofing attacks. */ final byte[] iv = encryptionContext.getCipher().getIV(); hmac.update(iv, 0, iv.length); } return makeCipherOutputForStream(httpOut, encryptionContext.getCipherDetails(), encryptionContext.getCipher(), hmac); }
From source file:com.joyent.manta.http.EncryptionHttpHelper.java
License:Open Source License
/** * Initializes an HMAC instance using the specified secret key. * * @param secretKey secret key to initialize with * @param hmac HMAC object to be initialized *//*from w ww . j av a2 s . co m*/ private static void initHmac(final SecretKey secretKey, final HMac hmac) { hmac.init(new KeyParameter(secretKey.getEncoded())); }
From source file:com.joyent.manta.serialization.HmacSerializerTest.java
License:Open Source License
private void canSerializeHmac(final String algorithm) throws Exception { final byte[] dataIn1 = new byte[] { (byte) 4, (byte) 83, (byte) 113, (byte) 66 }; final byte[] dataIn2 = new byte[] { (byte) 4, (byte) 83, (byte) 113, (byte) 66 }; final byte[] expected; final byte[] actual; {/*from ww w . jav a2s .c o m*/ HMac hmacExpected = SupportedHmacsLookupMap.INSTANCE.get(algorithm).get(); hmacExpected.init(new KeyParameter(this.secretKey.getEncoded())); hmacExpected.update(dataIn1, 0, dataIn1.length); hmacExpected.update(dataIn1, 0, dataIn2.length); expected = new byte[hmacExpected.getMacSize()]; actual = new byte[hmacExpected.getMacSize()]; hmacExpected.doFinal(expected, 0); } HMac hmac = SupportedHmacsLookupMap.INSTANCE.get(algorithm).get(); hmac.init(new KeyParameter(this.secretKey.getEncoded())); hmac.update(dataIn1, 0, dataIn1.length); final byte[] serializedContent; try (ByteArrayOutputStream out = new ByteArrayOutputStream(); Output output = new Output(out)) { kryo.writeObject(output, hmac); output.flush(); serializedContent = out.toByteArray(); } try (Input input = new FastInput(serializedContent)) { HMac deserialized = kryo.readObject(input, HMac.class); deserialized.update(dataIn2, 0, dataIn2.length); deserialized.doFinal(actual, 0); AssertJUnit.assertArrayEquals("Deserialized hmac couldn't compute equivalent value", expected, actual); } }
From source file:com.joyent.manta.util.HmacClonerTest.java
License:Open Source License
private void testHMacStateCanBeClonedAfterInitialization(SupportedCipherDetails cipherDetails, final String hmacName) { final SecretKey key = SecretKeyUtils.generate(cipherDetails); final HMac originalHmac = SupportedHmacsLookupMap.INSTANCE.get(hmacName).get(); originalHmac.init(new KeyParameter(key.getEncoded())); final HMac clonedHmac = new HmacCloner().createClone(originalHmac); final byte[] inputData = RandomUtils.nextBytes(cipherDetails.getBlockSizeInBytes() * 3); originalHmac.update(inputData, 0, inputData.length); clonedHmac.update(inputData, 0, inputData.length); final byte[] originalComputed = new byte[originalHmac.getMacSize()]; final byte[] clonedComputed = new byte[originalHmac.getMacSize()]; originalHmac.doFinal(originalComputed, 0); clonedHmac.doFinal(clonedComputed, 0); AssertJUnit.assertArrayEquals(originalComputed, clonedComputed); }
From source file:com.joyent.manta.util.HmacClonerTest.java
License:Open Source License
private void testHMacStateCanBeClonedAfterUse(final SupportedCipherDetails cipherDetails, final String hmacName) { final SecretKey key = SecretKeyUtils.generate(cipherDetails); final HMac originalHmac = SupportedHmacsLookupMap.INSTANCE.get(hmacName).get(); originalHmac.init(new KeyParameter(key.getEncoded())); final byte[] firstUpdate = RandomUtils.nextBytes(cipherDetails.getBlockSizeInBytes() * 3); originalHmac.update(firstUpdate, 0, firstUpdate.length); final HMac clonedHmac = new HmacCloner().createClone(originalHmac); final byte[] inputData = RandomUtils.nextBytes(cipherDetails.getBlockSizeInBytes() * 3); originalHmac.update(inputData, 0, inputData.length); clonedHmac.update(inputData, 0, inputData.length); final byte[] originalComputed = new byte[originalHmac.getMacSize()]; final byte[] clonedComputed = new byte[originalHmac.getMacSize()]; originalHmac.doFinal(originalComputed, 0); clonedHmac.doFinal(clonedComputed, 0); AssertJUnit.assertArrayEquals(originalComputed, clonedComputed); }
From source file:com.substanceofcode.twitter.XAuth.java
License:Apache License
public String getSignature(String message, String key) { try {//from ww w. java 2s. c o m HMac m = new HMac(new SHA1Digest()); m.init(new KeyParameter(key.getBytes("UTF-8"))); byte[] bytes = message.getBytes("UTF-8"); m.update(bytes, 0, bytes.length); byte[] mac = new byte[m.getMacSize()]; m.doFinal(mac, 0); String signature = new Base64().encode(mac); return signature; } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } return null; }