本文整理匯總了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);
}
示例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;
}
示例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();
}
示例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;
}
示例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")));
}
示例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());
}
示例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);
}
示例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());
}
示例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;
}
示例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));
}
示例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");
}
}
示例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);
}
}
示例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());
}
示例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());
}
示例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");
}
}