本文整理汇总了Java中org.bouncycastle.crypto.macs.HMac.update方法的典型用法代码示例。如果您正苦于以下问题:Java HMac.update方法的具体用法?Java HMac.update怎么用?Java HMac.update使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.macs.HMac
的用法示例。
在下文中一共展示了HMac.update方法的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: calculateRounds
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public static int calculateRounds(int milliseconds)
{
Logging.info("Calculating how many SHA1 rounds we can do in %d millis.", milliseconds);
HMac mac = new HMac(new SHA256Digest());
byte[] state = new byte[mac.getMacSize()];
long startTime = System.currentTimeMillis();
int pbkdf2Iterations = 0;
while((System.currentTimeMillis() - startTime) < milliseconds)
{
mac.update(state, 0, state.length);
mac.doFinal(state, 0);
pbkdf2Iterations++;
}
pbkdf2Iterations = Math.max(pbkdf2Iterations, PBKDF2Descriptor.MINIMUM_PBKD2_ITERS);
Logging.info("Got %d", pbkdf2Iterations);
return pbkdf2Iterations;
}
示例6: 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", "");
}
示例7: 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)));
}
}
示例8: 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);
}
示例9: 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));
}
示例10: 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;
}
示例11: main
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public static void main(
String[] args)
{
HMac gMac = new HMac(new GOST3411Digest(GOST28147Engine.getSBox("D-Test")));
gMac.init(new KeyParameter(PKCS5S1ParametersGenerator.PKCS5PasswordToUTF8Bytes("Boss".toCharArray())));
byte[] iBuf = new byte[4];
byte[] data = Hex.decode("b5d78fa546ba645c");
gMac.update(data, 0, data.length);
byte[] mac = new byte[gMac.getMacSize()];
int pos = 3;
while (++iBuf[pos] == 0)
{
--pos;
}
gMac.update(iBuf, 0, iBuf.length);
gMac.doFinal(mac, 0);
System.err.println(mac.length + " " + new String(Hex.encode(mac)));
PKCS5S2ParametersGenerator pGen = new PKCS5S2ParametersGenerator(new GOST3411Digest());
pGen.init(PKCS5S1ParametersGenerator.PKCS5PasswordToUTF8Bytes("1".toCharArray()), data, 2048);
KeyParameter kp = (KeyParameter)pGen.generateDerivedMacParameters(256);
System.err.println(kp.getKey().length + " " + new String(Hex.encode(kp.getKey())));
runTest(new GOST3411DigestTest());
}
示例12: genToken
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
/**
* Generates the current token. If the token can't be generated it returns
* an empty String.
*
* @return current token or an empty String
*/
protected static String genToken(final long counter, final HMac hmac, final int digits) {
if (hmac == null || digits <= 0) {
return "";
}
// generate 8 byte HOTP counter value (RFC 4226)
final byte msg[] = new byte[8];
for (int i = 0; i < 8; i++) {
msg[7 - i] = (byte) (counter >>> (i * 8));
}
// compute the HMAC
final byte[] hash = new byte[hmac.getMacSize()];
hmac.update(msg, 0, msg.length);
hmac.doFinal(hash, 0);
// Transform the HMAC to a HOTP value according to RFC 4226.
final int off = hash[hash.length - 1] & 0xF;
// Truncate the HMAC (look at RFC 4226 section 5.3, step 2).
int binary = ((hash[off] & 0x7f) << 24) | ((hash[off + 1] & 0xff) << 16) | ((hash[off + 2] & 0xff) << 8)
| ((hash[off + 3] & 0xff));
// use requested number of digits
final byte[] digitsArray = new byte[digits];
for (int i = 0; i < digits; i++) {
digitsArray[digits - 1 - i] = (byte) ('0' + (char) (binary % 10));
binary /= 10;
}
return new String(digitsArray, 0, digits);
}
示例13: performTest
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public void performTest()
{
super.performTest();
millionATest(million_a_digest);
HMac gMac = new HMac(new GOST3411Digest());
gMac.init(new KeyParameter(PKCS5S1ParametersGenerator.PKCS5PasswordToUTF8Bytes("1".toCharArray())));
byte[] data = "fred".getBytes();
gMac.update(data, 0, data.length);
byte[] mac = new byte[gMac.getMacSize()];
gMac.doFinal(mac, 0);
if (!Arrays.areEqual(Hex.decode("e9f98610cfc80084462b175a15d2b4ec10b2ab892eae5a6179d572d9b1db6b72"), mac))
{
fail("mac calculation failed.");
}
}
示例14: eciesDecrypt
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public ByteString eciesDecrypt(PrivateKey recipientPrivateKey, ByteString cipherText) {
BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) recipientPrivateKey;
ECNamedCurveSpec ecNamedCurveSpec = (ECNamedCurveSpec) bcecPrivateKey.getParams();
int level = SecurityLevel.from(ecNamedCurveSpec.getName()).size();
//cipherText = ephemeralPubKeyBytes + encryptedTokBytes + macBytes
//ephemeralPubKeyBytes = first ((384+7)/8)*2 + 1 bytes = first 97 bytes
//hmac is sha3_384 = 48 bytes or sha3_256 = 32 bytes
int ephemeralPubKeyLength = ((level + 7) / 8) * 2 + 1;
int hmacLength = level >> 3;
int cipherTextLength = cipherText.size();
if (cipherTextLength <= ephemeralPubKeyLength + hmacLength)
throw new RuntimeException(String.format("Illegal cipherText length: %d must be > %d", cipherTextLength, ephemeralPubKeyLength + hmacLength));
ByteString ephemeralPubKey = cipherText.substring(0, ephemeralPubKeyLength);
ByteString encryptedContent = cipherText.substring(ephemeralPubKeyLength, cipherTextLength - hmacLength);
ByteString hmac = cipherText.substring(cipherTextLength - hmacLength);
ECPrivateKeyParameters ecdhPrivateKeyParameters;
try {
ecdhPrivateKeyParameters = (ECPrivateKeyParameters) (PrivateKeyFactory.createKey(bcecPrivateKey.getEncoded()));
} catch (IOException e) {
logger.error("ECIES decrypt load private key exception", e);
throw new RuntimeException(e);
}
ECDomainParameters ecDomainParameters = ecdhPrivateKeyParameters.getParameters();
ECCurve ecCurve = ecDomainParameters.getCurve();
ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(ecCurve.decodePoint(ephemeralPubKey.toByteArray()), ecDomainParameters);
BasicAgreement agree = new ECDHBasicAgreement();
agree.init(ecdhPrivateKeyParameters);
byte[] keyAgreement = agree.calculateAgreement(ecPublicKeyParameters).toByteArray();
HKDFParameters hkdfParameters = new HKDFParameters(keyAgreement, null, null);
HKDFBytesGenerator hkdfBytesGenerator = new HKDFBytesGenerator(digest);
hkdfBytesGenerator.init(hkdfParameters);
byte[] hkdfOutputBytes = new byte[AESKEY_LENGTH + HMACKEY_LENGTH];
hkdfBytesGenerator.generateBytes(hkdfOutputBytes, 0, AESKEY_LENGTH + HMACKEY_LENGTH);
ByteString hkdfOutput = ByteString.copyFrom(hkdfOutputBytes);
ByteString aesKey = hkdfOutput.substring(0, AESKEY_LENGTH);
ByteString hmacKey = hkdfOutput.substring(AESKEY_LENGTH, AESKEY_LENGTH + HMACKEY_LENGTH);
HMac hMac = new HMac(digest);
hMac.init(new KeyParameter(hmacKey.toByteArray()));
hMac.update(encryptedContent.toByteArray(), 0, encryptedContent.size());
byte[] recoveredHmac = new byte[hMac.getMacSize()];
hMac.doFinal(recoveredHmac, 0);
if (!MessageDigest.isEqual(hmac.toByteArray(), recoveredHmac)) {
throw new RuntimeException("HMAC verify failed");
}
CFBBlockCipher aesCipher = new CFBBlockCipher(
new AESEngine(), BLOCK_BIT_SIZE);
ByteString iv = encryptedContent.substring(0, IV_LENGTH);
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(aesKey.toByteArray()), iv.toByteArray());
aesCipher.init(false, ivAndKey);
byte[] decryptedBytes = new byte[500];
aesCipher.decryptBlock(encryptedContent.substring(IV_LENGTH).toByteArray(), 0, decryptedBytes, 0);
return ByteString.copyFrom(decryptedBytes);
}
示例15: performTest
import org.bouncycastle.crypto.macs.HMac; //导入方法依赖的package包/类
public void performTest()
{
super.performTest();
millionATest(million_a_digest);
HMac gMac = new HMac(new GOST3411Digest());
gMac.init(new KeyParameter(PKCS5S1ParametersGenerator.PKCS5PasswordToUTF8Bytes("1".toCharArray())));
byte[] data = "fred".getBytes();
gMac.update(data, 0, data.length);
byte[] mac = new byte[gMac.getMacSize()];
gMac.doFinal(mac, 0);
System.err.println("e080de3bde792327a6cccfa5dfd51e72b6829baa88d8130ed1a48822873fc7f6");
System.err.println(new String(Hex.encode(mac)));
}