本文整理汇总了Java中org.bouncycastle.crypto.macs.HMac.init方法的典型用法代码示例。如果您正苦于以下问题:Java HMac.init方法的具体用法?Java HMac.init怎么用?Java HMac.init使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.macs.HMac
的用法示例。
在下文中一共展示了HMac.init方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: perform
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public TestResult perform()
{
HMac hmac = new HMac(new RIPEMD160Digest());
byte[] resBuf = new byte[hmac.getMacSize()];
for (int i = 0; i < messages.length; i++)
{
byte[] m = messages[i].getBytes();
if (messages[i].startsWith("0x"))
{
m = Hex.decode(messages[i].substring(2));
}
hmac.init(new KeyParameter(Hex.decode(keys[i])));
hmac.update(m, 0, m.length);
hmac.doFinal(resBuf, 0);
if (!Arrays.areEqual(resBuf, Hex.decode(digests[i])))
{
return new SimpleTestResult(false, getName() + ": Vector " + i + " failed");
}
}
return new SimpleTestResult(true, getName() + ": Okay");
}
示例2: hmac_hash
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
static void hmac_hash(Digest digest, byte[] secret, byte[] seed, byte[] out)
{
HMac mac = new HMac(digest);
KeyParameter param = new KeyParameter(secret);
byte[] a = seed;
int size = digest.getDigestSize();
int iterations = (out.length + size - 1) / size;
byte[] buf = new byte[mac.getMacSize()];
byte[] buf2 = new byte[mac.getMacSize()];
for (int i = 0; i < iterations; i++)
{
mac.init(param);
mac.update(a, 0, a.length);
mac.doFinal(buf, 0);
a = buf;
mac.init(param);
mac.update(a, 0, a.length);
mac.update(seed, 0, seed.length);
mac.doFinal(buf2, 0);
System.arraycopy(buf2, 0, out, (size * i), Math.min(size, out.length - (size * i)));
}
}
示例3: hmac_hash
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
static void hmac_hash(Digest digest, byte[] secret, byte[] seed, byte[] out)
{
HMac mac = new HMac(digest);
mac.init(new KeyParameter(secret));
byte[] a = seed;
int size = digest.getDigestSize();
int iterations = (out.length + size - 1) / size;
byte[] buf = new byte[mac.getMacSize()];
byte[] buf2 = new byte[mac.getMacSize()];
for (int i = 0; i < iterations; i++)
{
mac.update(a, 0, a.length);
mac.doFinal(buf, 0);
a = buf;
mac.update(a, 0, a.length);
mac.update(seed, 0, seed.length);
mac.doFinal(buf2, 0);
System.arraycopy(buf2, 0, out, (size * i), Math.min(size, out.length - (size * i)));
}
}
示例4: perform
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public TestResult perform()
{
HMac hmac = new HMac(new RIPEMD128Digest());
byte[] resBuf = new byte[hmac.getMacSize()];
for (int i = 0; i < messages.length; i++)
{
byte[] m = messages[i].getBytes();
if (messages[i].startsWith("0x"))
{
m = Hex.decode(messages[i].substring(2));
}
hmac.init(new KeyParameter(Hex.decode(keys[i])));
hmac.update(m, 0, m.length);
hmac.doFinal(resBuf, 0);
if (!Arrays.areEqual(resBuf, Hex.decode(digests[i])))
{
return new SimpleTestResult(false, getName() + ": Vector " + i + " failed");
}
}
return new SimpleTestResult(true, getName() + ": Okay");
}
示例5: generateHmac
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
/**
* Generate HMAC-SHA1 for message
*
* @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", "");
}
示例6: hmac_hash
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
private static void hmac_hash(Digest digest, byte[] secret, byte[] seed, byte[] out)
{
HMac mac = new HMac(digest);
KeyParameter param = new KeyParameter(secret);
byte[] a = seed;
int size = digest.getDigestSize();
int iterations = (out.length + size - 1) / size;
byte[] buf = new byte[mac.getMacSize()];
byte[] buf2 = new byte[mac.getMacSize()];
for (int i = 0; i < iterations; i++)
{
mac.init(param);
mac.update(a, 0, a.length);
mac.doFinal(buf, 0);
a = buf;
mac.init(param);
mac.update(a, 0, a.length);
mac.update(seed, 0, seed.length);
mac.doFinal(buf2, 0);
System.arraycopy(buf2, 0, out, (size * i), Math.min(size, out.length - (size * i)));
}
}
示例7: createMacCalculator
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
static MacCalculator createMacCalculator(final ASN1ObjectIdentifier digestAlgorithm, ExtendedDigest digest, final PKCS12PBEParams pbeParams, final char[] password)
{
PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(digest);
pGen.init(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password), pbeParams.getIV(), pbeParams.getIterations().intValue());
final KeyParameter keyParam = (KeyParameter)pGen.generateDerivedMacParameters(digest.getDigestSize() * 8);
final HMac hMac = new HMac(digest);
hMac.init(keyParam);
return new MacCalculator()
{
public AlgorithmIdentifier getAlgorithmIdentifier()
{
return new AlgorithmIdentifier(digestAlgorithm, pbeParams);
}
public OutputStream getOutputStream()
{
return new MacOutputStream(hMac);
}
public byte[] getMac()
{
byte[] res = new byte[hMac.getMacSize()];
hMac.doFinal(res, 0);
return res;
}
public GenericKey getKey()
{
return new GenericKey(getAlgorithmIdentifier(), PKCS12ParametersGenerator.PKCS12PasswordToBytes(password));
}
};
}
示例8: calculateMacTag
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
/**
* Calculates the MacTag (to be used for key confirmation), as defined by
* <a href="http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf">NIST SP 800-56A Revision 1</a>,
* Section 8.2 Unilateral Key Confirmation for Key Agreement Schemes.
* <p/>
* <p/>
* <pre>
* MacTag = HMAC(MacKey, MacLen, MacData)
*
* MacKey = H(K || "JPAKE_KC")
*
* MacData = "KC_1_U" || participantId || partnerParticipantId || gx1 || gx2 || gx3 || gx4
*
* Note that both participants use "KC_1_U" because the sender of the round 3 message
* is always the initiator for key confirmation.
*
* HMAC = {@link HMac} used with the given {@link Digest}
* H = The given {@link Digest}</li>
* MacLen = length of MacTag
* </pre>
* <p/>
*/
public static BigInteger calculateMacTag(
String participantId,
String partnerParticipantId,
BigInteger gx1,
BigInteger gx2,
BigInteger gx3,
BigInteger gx4,
BigInteger keyingMaterial,
Digest digest)
{
byte[] macKey = calculateMacKey(
keyingMaterial,
digest);
HMac mac = new HMac(digest);
byte[] macOutput = new byte[mac.getMacSize()];
mac.init(new KeyParameter(macKey));
/*
* MacData = "KC_1_U" || participantId_Alice || participantId_Bob || gx1 || gx2 || gx3 || gx4.
*/
updateMac(mac, "KC_1_U");
updateMac(mac, participantId);
updateMac(mac, partnerParticipantId);
updateMac(mac, gx1);
updateMac(mac, gx2);
updateMac(mac, gx3);
updateMac(mac, gx4);
mac.doFinal(macOutput, 0);
Arrays.fill(macKey, (byte)0);
return new BigInteger(macOutput);
}
示例9: getRFC5054Default
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
/**
* Create a {@link SimulatedTlsSRPIdentityManager} that implements the algorithm from RFC 5054 2.5.1.3
*
* @param group the {@link SRP6GroupParameters} defining the group that SRP is operating in
* @param seedKey the secret "seed key" referred to in RFC 5054 2.5.1.3
* @return an instance of {@link SimulatedTlsSRPIdentityManager}
*/
public static SimulatedTlsSRPIdentityManager getRFC5054Default(SRP6GroupParameters group, byte[] seedKey)
{
SRP6VerifierGenerator verifierGenerator = new SRP6VerifierGenerator();
verifierGenerator.init(group, TlsUtils.createHash(HashAlgorithm.sha1));
HMac mac = new HMac(TlsUtils.createHash(HashAlgorithm.sha1));
mac.init(new KeyParameter(seedKey));
return new SimulatedTlsSRPIdentityManager(group, verifierGenerator, mac);
}
示例10: calculateMacTag
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
/**
* Calculates the MacTag (to be used for key confirmation), as defined by
* <a href="http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf">NIST SP 800-56A Revision 1</a>,
* Section 8.2 Unilateral Key Confirmation for Key Agreement Schemes.
* <pre>
* MacTag = HMAC(MacKey, MacLen, MacData)
*
* MacKey = H(K || "JPAKE_KC")
*
* MacData = "KC_1_U" || participantId || partnerParticipantId || gx1 || gx2 || gx3 || gx4
*
* Note that both participants use "KC_1_U" because the sender of the round 3 message
* is always the initiator for key confirmation.
*
* HMAC = {@link HMac} used with the given {@link Digest}
* H = The given {@link Digest}
* MacLen = length of MacTag
* </pre>
*/
public static BigInteger calculateMacTag(
String participantId,
String partnerParticipantId,
BigInteger gx1,
BigInteger gx2,
BigInteger gx3,
BigInteger gx4,
BigInteger keyingMaterial,
Digest digest)
{
byte[] macKey = calculateMacKey(
keyingMaterial,
digest);
HMac mac = new HMac(digest);
byte[] macOutput = new byte[mac.getMacSize()];
mac.init(new KeyParameter(macKey));
/*
* MacData = "KC_1_U" || participantId_Alice || participantId_Bob || gx1 || gx2 || gx3 || gx4.
*/
updateMac(mac, "KC_1_U");
updateMac(mac, participantId);
updateMac(mac, partnerParticipantId);
updateMac(mac, gx1);
updateMac(mac, gx2);
updateMac(mac, gx3);
updateMac(mac, gx4);
mac.doFinal(macOutput, 0);
Arrays.fill(macKey, (byte)0);
return new BigInteger(macOutput);
}
示例11: OPE
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
/**
* Construct OPE with a key
* @param keyBytes Key bytes.
*/
public OPE(byte[] keyBytes) {
// get the key
key = new byte[keyBytes.length];
System.arraycopy(keyBytes, 0, key, 0, keyBytes.length);
VIL_PRF = new HMac(new SHA1Digest());
VIL_PRF.init(new KeyParameter(key));
this.cache = new Hashtable<Long, Long>(maxCacheSize);
}
示例12: Generate
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public static String Generate(HMac digest, byte[] pbSecret, long uFactor,
int uCodeDigits, boolean bAddChecksum, int iTruncationOffset)
{
byte[] pbText = MemUtil.UInt64ToBytes(uFactor);
StrUtil.ArraysReverse(pbText); // Big-Endian
KeyParameter key = new KeyParameter(pbSecret);
digest.init(key);
byte[] pbHash = new byte[digest.getMacSize()];
digest.update(pbText, 0, pbText.length);
digest.doFinal(pbHash, 0);
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);
}
示例13: bcHmacMD5
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public static void bcHmacMD5()
{
HMac hmac = new HMac(new MD5Digest());
// 必须是16进制的字符,长度必须是2的倍数
hmac.init(new KeyParameter(org.bouncycastle.util.encoders.Hex.decode("123456789abcde")));
hmac.update(src.getBytes(), 0, src.getBytes().length);
// 执行摘要
byte[] hmacMD5Bytes = new byte[hmac.getMacSize()];
hmac.doFinal(hmacMD5Bytes, 0);
System.out.println("bc hmacMD5:" + org.bouncycastle.util.encoders.Hex.toHexString(hmacMD5Bytes));
}
示例14: hmacSha512
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
/**
* Calculate the HMAC-SHA512 digest for use with BIP 32
*
* @param key Key
* @param input Bytes to be hashed
* @return Hashed result
*/
public static byte[] hmacSha512(byte[] key, byte[] input) {
HMac hmac = new HMac(new SHA512Digest());
hmac.init(new KeyParameter(key));
hmac.update(input, 0, input.length);
byte[] out = new byte[64];
hmac.doFinal(out, 0);
return out;
}
示例15: init
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public void init(final String base64Iv, final byte[] encKey, final byte[] hmacKey) {
iv = Base64.decodeBase64(base64Iv);
cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
encKeyParam = new KeyParameter(encKey);
mac = new HMac(new SHA1Digest());
mac.init(new KeyParameter(hmacKey));
}