当前位置: 首页>>代码示例>>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;未经允许,请勿转载。