当前位置: 首页>>代码示例>>Java>>正文


Java Mac.update方法代码示例

本文整理汇总了Java中javax.crypto.Mac.update方法的典型用法代码示例。如果您正苦于以下问题:Java Mac.update方法的具体用法?Java Mac.update怎么用?Java Mac.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.crypto.Mac的用法示例。


在下文中一共展示了Mac.update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: run

import javax.crypto.Mac; //导入方法依赖的package包/类
@Override
public void run(Provider p) throws Exception {
    Mac mac = Mac.getInstance(alg, p);
    SecretKey keySpec = new SecretKeySpec(key, alg);
    mac.init(keySpec);
    mac.update(input);
    byte[] macv = mac.doFinal();
    if (Arrays.equals(macvalue, macv) == false) {
        System.out.println("Mac test for " + alg + " failed:");
        if (input.length < 256) {
            System.out.println("input:       "
                    + PKCS11Test.toString(input));
        }
        System.out.println("key:        " + PKCS11Test.toString(key));
        System.out.println("macvalue:   "
                + PKCS11Test.toString(macvalue));
        System.out.println("calculated: " + PKCS11Test.toString(macv));
        throw new Exception("Mac test for " + alg + " failed");
    }
    System.out.println("passed: " + alg);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:22,代码来源:MacKAT.java

示例2: nullByteBufferTest

import javax.crypto.Mac; //导入方法依赖的package包/类
/**
 * NULL ByteBuffer test case. Pass NULL ByteBuffer to Mac.update(ByteBuffer
 * buffer) method. An IllegalArgumentException expected.
 *
 * @param theMac Mac object to test.
 * @return true - test case pass; false - otherwise.
 */
protected boolean nullByteBufferTest(Mac theMac) {
    try {
        ByteBuffer buf = null;
        theMac.update(buf);
        theMac.doFinal();
    } catch (IllegalArgumentException e) {
        // expected exception has been thrown
        return true;
    }

    System.out.println("FAIL: "
            + "IllegalArgumentException hasn't been thrown as expected");

    return false;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:PBMacBuffer.java

示例3: encryptHMACSha1

import javax.crypto.Mac; //导入方法依赖的package包/类
protected static byte[] encryptHMACSha1(byte[] data, byte[] key) throws
        NoSuchAlgorithmException, InvalidKeyException {
    SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(signingKey);
    mac.update(data);
    return mac.doFinal();
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:9,代码来源:AuthorizeHelper.java

示例4: calculateMac

import javax.crypto.Mac; //导入方法依赖的package包/类
private byte[] calculateMac(char[] passwd, byte[] data)
    throws IOException
{
    byte[] mData = null;
    String algName = "SHA1";

    try {
        // Generate a random salt.
        byte[] salt = getSalt();

        // generate MAC (MAC key is generated within JCE)
        Mac m = Mac.getInstance("HmacPBESHA1");
        PBEParameterSpec params =
                    new PBEParameterSpec(salt, iterationCount);
        SecretKey key = getPBEKey(passwd);
        m.init(key, params);
        m.update(data);
        byte[] macResult = m.doFinal();

        // encode as MacData
        MacData macData = new MacData(algName, macResult, salt,
                                            iterationCount);
        DerOutputStream bytes = new DerOutputStream();
        bytes.write(macData.getEncoded());
        mData = bytes.toByteArray();
    } catch (Exception e) {
        throw new IOException("calculateMac failed: " + e, e);
    }
    return mData;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:31,代码来源:PKCS12KeyStore.java

示例5: main

import javax.crypto.Mac; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {

        Mac mac;

        // Test vectors obtained from
        // https://groups.google.com/d/msg/sci.crypt/OolWgsgQD-8/IUR2KhCcfEkJ
        mac = Mac.getInstance("HmacSHA512/224");
        mac.init(new SecretKeySpec(xeh("4a656665"), "HmacSHA512/224"));
        mac.update("what do ya want for nothing?".getBytes());
        Asserts.assertTrue(Arrays.equals(mac.doFinal(),
                xeh("4a530b31a79ebcce36916546317c45f247d83241dfb818fd37254bde")));

        mac = Mac.getInstance("HmacSHA512/256");
        mac.init(new SecretKeySpec(xeh("4a656665"), "HmacSHA512/256"));
        mac.update("what do ya want for nothing?".getBytes());
        Asserts.assertTrue(Arrays.equals(mac.doFinal(),
                xeh("6df7b24630d5ccb2ee335407081a87188c221489768fa2020513b2d593359456")));

        mac = Mac.getInstance("HmacSHA512/224");
        mac.init(new SecretKeySpec(xeh("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
                "HmacSHA512/224"));
        mac.update("Hi There".getBytes());
        Asserts.assertTrue(Arrays.equals(mac.doFinal(),
                xeh("b244ba01307c0e7a8ccaad13b1067a4cf6b961fe0c6a20bda3d92039")));

        mac = Mac.getInstance("HmacSHA512/256");
        mac.init(new SecretKeySpec(xeh("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
                "HmacSHA512/256"));
        mac.update("Hi There".getBytes());
        Asserts.assertTrue(Arrays.equals(mac.doFinal(),
                xeh("9f9126c3d9c3c330d760425ca8a217e31feae31bfe70196ff81642b868402eab")));
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:33,代码来源:HmacSHA512.java

示例6: testKnownInputs

import javax.crypto.Mac; //导入方法依赖的package包/类
public void testKnownInputs() throws Exception {
  String knownOutput = "9753980fe94daa8ecaa82216519393a9";
  String input = "The quick brown fox jumps over the lazy dog";

  Mac mac = Mac.getInstance("HmacMD5");
  mac.init(MD5_KEY);
  mac.update(input.getBytes(UTF_8));
  assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
  assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal(input.getBytes(UTF_8))).toString());
  assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).hashString(input, UTF_8).toString());
  assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).hashBytes(input.getBytes(UTF_8)).toString());
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:13,代码来源:MacHashFunctionTest.java

示例7: Hmac256

import javax.crypto.Mac; //导入方法依赖的package包/类
public static String Hmac256(String key, String data) throws Exception {
    Mac mac = Mac.getInstance("HmacSHA256");
    FileInputStream fis = new FileInputStream(data);
    SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
    mac.init(secret_key);

    byte[] buff = new byte[1];
    while ((fis.read(buff)) > 0) {
        mac.update((byte) buff[0]);
    }
    byte[] macbytes = mac.doFinal();
    return getHex(macbytes);
    //return getHex(buff);
}
 
开发者ID:individuoestima,项目名称:Security,代码行数:15,代码来源:SI.java

示例8: testKnownInputs_mixedAlgorithms

import javax.crypto.Mac; //导入方法依赖的package包/类
public void testKnownInputs_mixedAlgorithms() throws Exception {
  String knownOutput = "9753980fe94daa8ecaa82216519393a9";
  String input = "The quick brown fox jumps over the lazy dog";

  Mac mac = Mac.getInstance("HmacMD5");
  mac.init(SHA1_KEY);
  mac.update(input.getBytes(UTF_8));
  assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
  assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal(input.getBytes(UTF_8))).toString());
  assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).hashString(input, UTF_8).toString());
  assertEquals(knownOutput,
      Hashing.hmacMd5(SHA1_KEY).hashBytes(input.getBytes(UTF_8)).toString());
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:14,代码来源:MacHashFunctionTest.java

示例9: initializeCipher

import javax.crypto.Mac; //导入方法依赖的package包/类
private Cipher initializeCipher(Mac mac, SecretKeySpec key) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.ENCRYPT_MODE, key);

  byte[] ivBytes = cipher.getIV();
  mac.update(ivBytes);
  super.write(ivBytes, 0, ivBytes.length);

  return cipher;
}
 
开发者ID:CableIM,项目名称:Cable-Android,代码行数:11,代码来源:EncryptingPartOutputStream.java

示例10: assertMacHashing

import javax.crypto.Mac; //导入方法依赖的package包/类
private static void assertMacHashing(
    byte[] input, String algorithm, SecretKey key, HashFunction hashFunc) throws Exception {
  Mac mac = Mac.getInstance(algorithm);
  mac.init(key);
  mac.update(input);

  assertEquals(HashCode.fromBytes(mac.doFinal()), hashFunc.hashBytes(input));
  assertEquals(HashCode.fromBytes(mac.doFinal(input)), hashFunc.hashBytes(input));
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:10,代码来源:MacHashFunctionTest.java

示例11: 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:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:40,代码来源:MacSameTest.java

示例12: hmacSignature

import javax.crypto.Mac; //导入方法依赖的package包/类
/**
 * {@code signature = hmac(integrityKey, payload || initVector)}
 */
private static int hmacSignature(byte[] workBytes, String ikey) {
	try {
		Mac integrityHmac = createMac();
		integrityHmac
				.init(new SecretKeySpec(ikey.getBytes(), KEY_ALGORITHM));
		integrityHmac.update(workBytes, PAYLOAD_BASE, workBytes.length
				- OVERHEAD_SIZE);
		integrityHmac.update(workBytes, INITV_BASE, INITV_SIZE);
		return fromByteArray(integrityHmac.doFinal());
	} catch (InvalidKeyException e) {
		throw new IllegalStateException(e);
	}
}
 
开发者ID:wjggwm,项目名称:webside,代码行数:17,代码来源:SHA1HMAC.java

示例13: testMultipleUpdatesDoFinal

import javax.crypto.Mac; //导入方法依赖的package包/类
public void testMultipleUpdatesDoFinal() throws Exception {
  Mac mac = Mac.getInstance("HmacSHA1");
  mac.init(SHA1_KEY);
  mac.update("hello".getBytes(UTF_8));
  mac.update("world".getBytes(UTF_8));

  assertEquals(
      HashCode.fromBytes(mac.doFinal("!!!".getBytes(UTF_8))),
      Hashing.hmacSha1(SHA1_KEY).newHasher()
          .putString("hello", UTF_8)
          .putString("world", UTF_8)
          .putString("!!!", UTF_8)
          .hash());
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:15,代码来源:MacHashFunctionTest.java

示例14: testMultipleUpdates

import javax.crypto.Mac; //导入方法依赖的package包/类
public void testMultipleUpdates() throws Exception {
  Mac mac = Mac.getInstance("HmacSHA1");
  mac.init(SHA1_KEY);
  mac.update("hello".getBytes(UTF_8));
  mac.update("world".getBytes(UTF_8));

  assertEquals(
      HashCode.fromBytes(mac.doFinal()),
      Hashing.hmacSha1(SHA1_KEY).newHasher()
          .putString("hello", UTF_8)
          .putString("world", UTF_8)
          .hash());
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:14,代码来源:MacHashFunctionTest.java

示例15: 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


注:本文中的javax.crypto.Mac.update方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。