本文整理匯總了Java中javax.crypto.Mac類的典型用法代碼示例。如果您正苦於以下問題:Java Mac類的具體用法?Java Mac怎麽用?Java Mac使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Mac類屬於javax.crypto包,在下文中一共展示了Mac類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: sign
import javax.crypto.Mac; //導入依賴的package包/類
/**
* @brief 簽名
* @author [email protected]
* @date 2014-08-13 21:07:27
*
* @param signStr 被加密串
* @param secret 加密密鑰
*
* @return
*/
public static String sign(String signStr, String secret)
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException
{
String sig = null;
Mac mac = Mac.getInstance(HMAC_ALGORITHM);
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(CONTENT_CHARSET), mac.getAlgorithm());
mac.init(secretKey);
byte[] hash = mac.doFinal(signStr.getBytes(CONTENT_CHARSET));
// base64
//sig = new String(new BASE64Encoder().encode(hash).getBytes());
//sig = new String(Base64.encodeBase64(hash));
sig = new String(Base64.encode(hash));
return sig;
}
示例2: 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);
}
示例3: computeAccessSignature
import javax.crypto.Mac; //導入依賴的package包/類
private String computeAccessSignature(String timestamp, HttpMethod method, String urlTxt, ByteBuffer body)
throws GeneralSecurityException {
if (conn == null) {
throw new IllegalStateException("cannot generate exchange request post-disconnect()");
}
String prehash = timestamp + method.name() + urlTxt;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
mac.update(prehash.getBytes());
if (body != null) {
mac.update(body);
}
return new String(Base64.encodeBase64(mac.doFinal()));
}
示例4: hkdfExpand
import javax.crypto.Mac; //導入依賴的package包/類
public static byte[] hkdfExpand(byte[] prk, byte[] info, int len) throws NoSuchAlgorithmException, InvalidKeyException {
Mac hmacHasher = makeHMACHasher(prk);
byte[] T = {};
byte[] Tn = {};
int iterations = (int) Math.ceil(((double)len) / (BLOCKSIZE));
for (int i = 0; i < iterations; i++) {
Tn = digestBytes(Utils.concatAll(Tn, info, Utils.hex2Byte(Integer.toHexString(i + 1))),
hmacHasher);
T = Utils.concatAll(T, Tn);
}
byte[] result = new byte[len];
System.arraycopy(T, 0, result, 0, len);
return result;
}
示例5: computeSignature
import javax.crypto.Mac; //導入依賴的package包/類
private byte[] computeSignature(String baseString)
throws GeneralSecurityException, UnsupportedEncodingException {
SecretKey key = null;
synchronized (this) {
if (this.key == null) {
String keyString = OAuth.percentEncode(getConsumerSecret())
+ '&' + OAuth.percentEncode(getTokenSecret());
byte[] keyBytes = keyString.getBytes(ENCODING);
this.key = new SecretKeySpec(keyBytes, MAC_NAME);
}
key = this.key;
}
Mac mac = Mac.getInstance(MAC_NAME);
mac.init(key);
byte[] text = baseString.getBytes(ENCODING);
return mac.doFinal(text);
}
示例6: hmacTemplate
import javax.crypto.Mac; //導入依賴的package包/類
/**
* Hmac加密模板
*
* @param data 數據
* @param key 秘鑰
* @param algorithm 加密算法
* @return 密文字節數組
*/
private static byte[] hmacTemplate(byte[] data, byte[] key, String algorithm)
{
if(data == null || data.length == 0 || key == null || key.length == 0) return null;
try
{
SecretKeySpec secretKey = new SecretKeySpec(key, algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(secretKey);
return mac.doFinal(data);
} catch(InvalidKeyException | NoSuchAlgorithmException e)
{
e.printStackTrace();
return null;
}
}
示例7: computeHash
import javax.crypto.Mac; //導入依賴的package包/類
/**
* Computes the hmacSha256 hash of the data using the key given
* @param keyBytes The key to use to calculate the hash
* @param dataBytes The data to use to calculate the hash
* @return The hmacSha256 hash of the data using the key
*/
private static byte[] computeHash(byte[] keyBytes, byte[] dataBytes)
{
try
{
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec key = new SecretKeySpec(keyBytes, "HmacSHA256");
hmacSha256.init(key);
byte[] b = hmacSha256.doFinal(dataBytes);
return b;
}
catch( Exception e )
{
e.printStackTrace();
}
return null;
}
示例8: doTest
import javax.crypto.Mac; //導入依賴的package包/類
/**
* Tests Mac.update(ByteBuffer input) method. Three test cases are
* performed: - large ByteBuffer test case to test if the update() method
* process a large ByteBuffer correctly; - empty ByteBuffer test case to
* test if the update() method process an empty ByteBuffer correctly; - NULL
* ByteBuffer test case to test if the update() method throws expected
* IllegalArgumentException exception.
*
* @param theMacAlgo PBMAC algorithm to test
* @param thePBKDF2Algo PBKDF2 algorithm to test
* @return true - test passed; false - otherwise.
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws InvalidKeySpecException
* @see javax.crypto.Mac
*/
protected boolean doTest(String theMacAlgo, String thePBKDF2Algo)
throws NoSuchAlgorithmException, InvalidKeyException,
InvalidKeySpecException {
// obtain a SecretKey using PBKDF2
SecretKey key = getSecretKey(thePBKDF2Algo);
// Instantiate Mac object and init it with a SecretKey
Mac theMac = Mac.getInstance(theMacAlgo);
theMac.init(key);
// Do large ByteBuffer test case
if (!largeByteBufferTest(theMac)) {
System.out.println("Large ByteBuffer test case failed.");
return false;
}
// Do empty ByteBuffer test case
if (!emptyByteBufferTest(theMac)) {
System.out.println("Empty ByteBuffer test case failed.");
return false;
}
// Do null ByteBuffer test case
if (!nullByteBufferTest(theMac)) {
System.out.println("NULL ByteBuffer test case failed.");
return false;
}
return true;
}
示例9: calculateHash
import javax.crypto.Mac; //導入依賴的package包/類
static String calculateHash(String secret, String url, String algorithm) throws InvalidKeyException, NoSuchAlgorithmException {
Mac shaHmac = Mac.getInstance(algorithm);
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), algorithm);
shaHmac.init(secretKey);
byte[] hash = shaHmac.doFinal(url.getBytes());
return Hex.encodeHexString(hash);
}
示例10: hmacTemplate
import javax.crypto.Mac; //導入依賴的package包/類
/**
* Hmac加密模板
*
* @param data 數據
* @param key 秘鑰
* @param algorithm 加密算法
* @return 密文字節數組
*/
private static byte[] hmacTemplate(byte[] data, byte[] key, String algorithm) {
if (data == null || data.length == 0 || key == null || key.length == 0) return null;
try {
SecretKeySpec secretKey = new SecretKeySpec(key, algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(secretKey);
return mac.doFinal(data);
} catch (InvalidKeyException | NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
示例11: encodeHmacSHA512
import javax.crypto.Mac; //導入依賴的package包/類
/**
* HmacSHA512加密
*
* @param data 待加密數據
* @param key 密鑰
* @return byte[] 消息摘要
* @throws Exception
*/
public static byte[] encodeHmacSHA512(byte[] data, byte[] key) throws Exception {
// 還原密鑰
SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
// 實例化Mac
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
// 初始化Mac
mac.init(secretKey);
// 執行消息摘要
return mac.doFinal(data);
}
示例12: doHMACSHA256
import javax.crypto.Mac; //導入依賴的package包/類
private static String doHMACSHA256(String part1AndPart2, String secretKey) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secretKey.getBytes(), "HmacSHA256"));
byte[] hashBytes = mac.doFinal(part1AndPart2.getBytes());
String hash = doBASE64(hashBytes);
return hash;
}
示例13: encodeHmacMD4
import javax.crypto.Mac; //導入依賴的package包/類
/**
* HmacMD4消息摘要
*
* @param data 待做消息摘要處理的數據
* @param byte[] 密鑰
* @return byte[] 消息摘要
* @throws Exception
*/
public static byte[] encodeHmacMD4(byte[] data, byte[] key) throws Exception {
// 還原密鑰
SecretKey secretKey = new SecretKeySpec(key, "HmacMD4");
// 實例化Mac
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
// 初始化Mac
mac.init(secretKey);
// 執行消息摘要
return mac.doFinal(data);
}
示例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: getSignature
import javax.crypto.Mac; //導入依賴的package包/類
private String getSignature(String key, String text) {
try {
SecretKey secretKey = new SecretKeySpec(key.getBytes(ENC), HMAC_SHA1);
Mac mac = Mac.getInstance(HMAC_SHA1);
mac.init(secretKey);
byte[] result = mac.doFinal(text.getBytes(ENC));
return Base64.encodeToString(result, Base64.NO_WRAP);
} catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException e) {
return "";
}
}