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


Java Mac類代碼示例

本文整理匯總了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;
}
 
開發者ID:BennyThink,項目名稱:qcloudClient,代碼行數:29,代碼來源:Sign.java

示例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);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:22,代碼來源:MacKAT.java

示例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()));
}
 
開發者ID:cloudwall,項目名稱:libcwfincore,代碼行數:17,代碼來源:GdaxExchangeSession.java

示例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;
}
 
開發者ID:mozilla-mobile,項目名稱:FirefoxData-android,代碼行數:18,代碼來源:HKDF.java

示例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);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:18,代碼來源:HMAC_SHA1.java

示例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;
    }
}
 
開發者ID:Ayvytr,項目名稱:EasyAndroid,代碼行數:24,代碼來源:EncryptTool.java

示例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;
}
 
開發者ID:equella,項目名稱:Equella,代碼行數:25,代碼來源:D2LSigner.java

示例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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:47,代碼來源:PBMacBuffer.java

示例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);
}
 
開發者ID:CCob,項目名稱:bittrex4j,代碼行數:8,代碼來源:EncryptionUtility.java

示例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;
    }
}
 
開發者ID:tututututututu,項目名稱:BaseCore,代碼行數:21,代碼來源:EncryptUtils.java

示例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);
}
 
開發者ID:babymm,項目名稱:mumu,代碼行數:19,代碼來源:HmacCoder.java

示例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;
}
 
開發者ID:rahmanusta,項目名稱:JwtDemo,代碼行數:9,代碼來源:JwtApp.java

示例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);
}
 
開發者ID:mumucommon,項目名稱:mumu-core,代碼行數:19,代碼來源:HmacCoder.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: 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 "";
    }
}
 
開發者ID:mingdroid,項目名稱:tumbviewer,代碼行數:13,代碼來源:OAuth1SigningHelper.java


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