當前位置: 首頁>>代碼示例>>Java>>正文


Java HMac.getMacSize方法代碼示例

本文整理匯總了Java中org.bouncycastle.crypto.macs.HMac.getMacSize方法的典型用法代碼示例。如果您正苦於以下問題:Java HMac.getMacSize方法的具體用法?Java HMac.getMacSize怎麽用?Java HMac.getMacSize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.bouncycastle.crypto.macs.HMac的用法示例。


在下文中一共展示了HMac.getMacSize方法的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");
}
 
開發者ID:ttt43ttt,項目名稱:gwt-crypto,代碼行數:25,代碼來源:RIPEMD160HMacTest.java

示例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)));
    }
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:23,代碼來源:TlsUtils.java

示例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)));
    }
}
 
開發者ID:ttt43ttt,項目名稱:gwt-crypto,代碼行數:21,代碼來源:TlsUtils.java

示例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");
}
 
開發者ID:ttt43ttt,項目名稱:gwt-crypto,代碼行數:25,代碼來源:RIPEMD128HMacTest.java

示例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;
}
 
開發者ID:AstromechZA,項目名稱:bunkr,代碼行數:18,代碼來源:PBKDF2Descriptor.java

示例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)));
    }
}
 
開發者ID:coova,項目名稱:jradius,代碼行數:23,代碼來源:TlsUtils.java

示例7: 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", "");
}
 
開發者ID:entertailion,項目名稱:Android-Shapeways,代碼行數:25,代碼來源:Request.java

示例8: 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));
        }
    };
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:40,代碼來源:PKCS12PBEUtils.java

示例9: 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);

}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:59,代碼來源:JPAKEUtil.java

示例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);

}
 
開發者ID:ttt43ttt,項目名稱:gwt-crypto,代碼行數:56,代碼來源:JPAKEUtil.java

示例11: 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);
}
 
開發者ID:pfn,項目名稱:keepassj,代碼行數:29,代碼來源:HmacOtp.java

示例12: 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));
	
}
 
開發者ID:Tim9Liu9,項目名稱:java_security,代碼行數:14,代碼來源:HMACTest.java

示例13: 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());
}
 
開發者ID:credentials,項目名稱:irma_future_id,代碼行數:34,代碼來源:GOST3411DigestTest.java

示例14: 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);
}
 
開發者ID:kwart,項目名稱:totp-me,代碼行數:37,代碼來源:TOTPMIDlet.java

示例15: PKCS5S2ParametersGenerator

import org.bouncycastle.crypto.macs.HMac; //導入方法依賴的package包/類
public PKCS5S2ParametersGenerator(Digest digest)
{
    hMac = new HMac(digest);
    state = new byte[hMac.getMacSize()];
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:6,代碼來源:PKCS5S2ParametersGenerator.java


注:本文中的org.bouncycastle.crypto.macs.HMac.getMacSize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。