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


Java Mac.reset方法代碼示例

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


在下文中一共展示了Mac.reset方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: F

import javax.crypto.Mac; //導入方法依賴的package包/類
private static void F(Mac mac, byte[] S, int c, int i, byte[] dst, int offset, int len) throws javax.crypto.ShortBufferException
{
	// U_1 = PRF(P, S || INT (i))
	mac.reset();
	mac.update(S);
	mac.update(INT(i));

	byte[] U = mac.doFinal();
	byte[] T = U.clone();

	// U_j = PRF(P, U_{j-1})
	// T_i = F(P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
	for(int j = 1; j < c; j++)
	{
		mac.update(U);
		mac.doFinal(U, 0);
		XOR(T, U, len);
	}
	System.arraycopy(T, 0, dst, offset, len);
}
 
開發者ID:monolifed,項目名稱:mininoteview,代碼行數:21,代碼來源:Pbkdf2.java

示例2: doTest

import javax.crypto.Mac; //導入方法依賴的package包/類
@Override
public void doTest(String algo) throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeyException {
    Mac mac;
    try {
        mac = Mac.getInstance(algo, "SunJCE");
    } catch (NoSuchAlgorithmException nsae) {
        // depending on Solaris configuration,
        // it can support HMAC or not with Mac
        System.out.println("Expected NoSuchAlgorithmException thrown: "
                + nsae);
        return;
    }

    byte[] plain = new byte[MESSAGE_SIZE];
    for (int i = 0; i < MESSAGE_SIZE; i++) {
        plain[i] = (byte) (i % 256);
    }

    byte[] tail = new byte[plain.length - OFFSET];
    System.arraycopy(plain, OFFSET, tail, 0, tail.length);

    SecureRandom srdm = new SecureRandom();
    byte[] keyVal = new byte[KEY_SIZE];
    srdm.nextBytes(keyVal);
    SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC");

    mac.init(keySpec);
    byte[] result1 = mac.doFinal(plain);

    mac.reset();
    mac.update(plain[0]);
    mac.update(plain, 1, OFFSET - 1);
    byte[] result2 = mac.doFinal(tail);

    if (!java.util.Arrays.equals(result1, result2)) {
        throw new RuntimeException("result1 and result2 are not the same");
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:40,代碼來源:MacSameTest.java

示例3: main

import javax.crypto.Mac; //導入方法依賴的package包/類
@Override
public void main(Provider p) throws Exception {
    if (p.getService("Mac", "HmacMD5") == null) {
        System.out.println(p + " does not support HmacMD5, skipping");
        return;
    }
    Random random = new Random();
    byte[] data1 = new byte[10 * 1024];
    random.nextBytes(data1);
    byte[] keyData = new byte[16];
    random.nextBytes(keyData);
    SecretKeySpec key = new SecretKeySpec(keyData, "Hmac");
    Mac mac = Mac.getInstance("HmacMD5", p);
    mac.init(key);
    mac.init(key);
    mac.update(data1);
    mac.init(key);
    mac.doFinal();
    mac.doFinal();
    mac.update(data1);
    mac.doFinal();
    mac.reset();
    mac.reset();
    mac.init(key);
    mac.reset();
    mac.update(data1);
    mac.reset();

    System.out.println("All tests passed");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:31,代碼來源:ReinitMac.java

示例4: digestBytes

import javax.crypto.Mac; //導入方法依賴的package包/類
public static byte[] digestBytes(byte[] message, Mac hasher) {
  hasher.update(message);
  byte[] ret = hasher.doFinal();
  hasher.reset();
  return ret;
}
 
開發者ID:mozilla-mobile,項目名稱:FirefoxData-android,代碼行數:7,代碼來源:HKDF.java

示例5: xorPayloadToHmacPad

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * {@code payload = payload ^ hmac(encryptionKey, initVector || counterBytes)}
 * per max-20-byte blocks.
 */
private static void xorPayloadToHmacPad(byte[] workBytes, String ekey) {
	int payloadSize = workBytes.length - OVERHEAD_SIZE;
	int sections = (payloadSize + COUNTER_PAGESIZE - 1) / COUNTER_PAGESIZE;
	if (sections > COUNTER_SECTIONS) {
		throw new IllegalArgumentException(String.format(
				"Payload is %s bytes, exceeds limit of %s", payloadSize,
				COUNTER_PAGESIZE * COUNTER_SECTIONS));
	}

	Mac encryptionHmac = createMac();

	byte[] pad = new byte[COUNTER_PAGESIZE + 3];
	int counterSize = 0;

	for (int section = 0; section < sections; ++section) {
		int sectionBase = section * COUNTER_PAGESIZE;
		int sectionSize = min(payloadSize - sectionBase, COUNTER_PAGESIZE);

		try {
			encryptionHmac.reset();
			encryptionHmac.init(new SecretKeySpec(ekey.getBytes(),
					KEY_ALGORITHM));
			encryptionHmac.update(workBytes, INITV_BASE, INITV_SIZE);
			if (counterSize != 0) {
				encryptionHmac.update(pad, COUNTER_PAGESIZE, counterSize);
			}
			encryptionHmac.doFinal(pad, 0);
		} catch (ShortBufferException | InvalidKeyException e) {
			throw new IllegalStateException(e);
		}

		for (int i = 0; i < sectionSize; ++i) {
			workBytes[PAYLOAD_BASE + sectionBase + i] ^= pad[i];
		}

		Arrays.fill(pad, 0, COUNTER_PAGESIZE, (byte) 0);

		if (counterSize == 0
				|| ++pad[COUNTER_PAGESIZE + counterSize - 1] == 0) {
			++counterSize;
		}
	}
}
 
開發者ID:wjggwm,項目名稱:webside,代碼行數:48,代碼來源:SHA1HMAC.java

示例6: doTest

import javax.crypto.Mac; //導入方法依賴的package包/類
private void doTest(String algo, Provider provider)
        throws NoSuchAlgorithmException, NoSuchProviderException,
        InvalidKeyException {
    System.out.println("Test " + algo);
    Mac mac;
    try {
        mac = Mac.getInstance(algo, provider);
    } catch (NoSuchAlgorithmException nsae) {
        if ("SunPKCS11-Solaris".equals(provider.getName())) {
            // depending on Solaris configuration,
            // it can support HMAC or not with Mac
            System.out.println("Expected NoSuchAlgorithmException thrown: "
                    + nsae);
            return;
        }
        throw nsae;
    }

    byte[] plain = new byte[MESSAGE_SIZE];
    for (int i = 0; i < MESSAGE_SIZE; i++) {
        plain[i] = (byte) (i % 256);
    }

    byte[] tail = new byte[plain.length - OFFSET];
    System.arraycopy(plain, OFFSET, tail, 0, tail.length);

    SecureRandom srdm = new SecureRandom();
    byte[] keyVal = new byte[KEY_SIZE];
    srdm.nextBytes(keyVal);
    SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC");

    mac.init(keySpec);
    byte[] result1 = mac.doFinal(plain);

    mac.reset();
    mac.update(plain[0]);
    mac.update(plain, 1, OFFSET - 1);
    byte[] result2 = mac.doFinal(tail);

    if (!java.util.Arrays.equals(result1, result2)) {
        throw new RuntimeException("result1 and result2 are not the same");
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:44,代碼來源:MacSameTest.java

示例7: doTest

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * Uses a random generator to initialize a message, instantiate a Mac object
 * according to the given PBMAC1 algorithm, initialize the object with a
 * SecretKey derived using PBKDF2 algorithm (see PKCS #5 v21, chapter 7.1),
 * feed the message into the Mac object all at once and get the output MAC
 * as result1. Reset the Mac object, chop the message into three pieces,
 * feed into the Mac object sequentially, and get the output MAC as result2.
 * Finally, compare result1 and result2 and see if they are the same.
 *
 * @param theMacAlgo PBMAC algorithm to test
 * @param thePBKDF2Algo PBKDF2 algorithm to test
 * @return true - the test is passed; false - otherwise.
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws InvalidKeySpecException
 */
protected boolean doTest(String theMacAlgo, String thePBKDF2Algo)
        throws NoSuchAlgorithmException, InvalidKeyException,
        InvalidKeySpecException {
    int OFFSET = 5;

    // Some message for which a MAC result will be calculated
    byte[] plain = new byte[25];
    new SecureRandom().nextBytes(plain);

    // Form tail - is one of the three pieces
    byte[] tail = new byte[plain.length - OFFSET];
    System.arraycopy(plain, OFFSET, tail, 0, tail.length);

    // Obtain a SecretKey using PBKDF2
    SecretKey key = getSecretKey(thePBKDF2Algo);

    // Instantiate Mac object and init it with a SecretKey and calc result1
    Mac theMac = Mac.getInstance(theMacAlgo);
    theMac.init(key);
    byte[] result1 = theMac.doFinal(plain);

    if (!isMacLengthExpected(theMacAlgo, result1.length)) {
        return false;
    }

    // Reset Mac and calculate result2
    theMac.reset();
    theMac.update(plain[0]);
    theMac.update(plain, 1, OFFSET - 1);
    byte[] result2 = theMac.doFinal(tail);

    // Return result
    if (!java.util.Arrays.equals(result1, result2)) {
        System.out.println("result1 and result2 are not the same:");
        System.out.println("result1: " + dumpByteArray(result1));
        System.out.println("result2: " + dumpByteArray(result2));
        return false;
    } else {
        System.out.println("Resulted MAC with update and doFinal is same");
    }

    return true;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:60,代碼來源:PBMacDoFinalVsUpdate.java

示例8: updateHmac

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * Updates the given {@link Mac}. This generates a digest for valueToDigest and the key the Mac was initialized
 *
 * @param mac
 *            the initialized {@link Mac} to update
 * @param valueToDigest
 *            the value to update the {@link Mac} with
 *            <p>
 *            The InputStream must not be null and will not be closed
 *            </p>
 * @return the updated {@link Mac}
 * @throws IOException
 *             If an I/O error occurs.
 * @throws IllegalStateException
 *             If the Mac was not initialized
 * @since 1.x
 */
public static Mac updateHmac(final Mac mac, final InputStream valueToDigest) throws IOException {
    mac.reset();
    final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
    int read = valueToDigest.read(buffer, 0, STREAM_BUFFER_LENGTH);

    while (read > -1) {
        mac.update(buffer, 0, read);
        read = valueToDigest.read(buffer, 0, STREAM_BUFFER_LENGTH);
    }

    return mac;
}
 
開發者ID:gusavila92,項目名稱:java-android-websocket-client,代碼行數:30,代碼來源:HmacUtils.java

示例9: updateHmac

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * Resets and then updates the given {@link Mac} with the value.
 *
 * @param mac
 *            the initialized {@link Mac} to update
 * @param valueToDigest
 *            the value to update the {@link Mac} with
 *            <p>
 *            The InputStream must not be null and will not be closed
 *            </p>
 * @return the updated {@link Mac}
 * @throws IOException
 *             If an I/O error occurs.
 * @throws IllegalStateException
 *             If the Mac was not initialized
 */
public static Mac updateHmac(final Mac mac, final InputStream valueToDigest) throws IOException {
    mac.reset();
    final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
    int read = valueToDigest.read(buffer, 0, STREAM_BUFFER_LENGTH);

    while (read > -1) {
        mac.update(buffer, 0, read);
        read = valueToDigest.read(buffer, 0, STREAM_BUFFER_LENGTH);
    }

    return mac;
}
 
開發者ID:HTBridge,項目名稱:pivaa,代碼行數:29,代碼來源:HmacUtils.java


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