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


Java Mac.init方法代碼示例

本文整理匯總了Java中javax.crypto.Mac.init方法的典型用法代碼示例。如果您正苦於以下問題:Java Mac.init方法的具體用法?Java Mac.init怎麽用?Java Mac.init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.crypto.Mac的用法示例。


在下文中一共展示了Mac.init方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: signRequest

import javax.crypto.Mac; //導入方法依賴的package包/類
public String signRequest(String path, String query) throws NoSuchAlgorithmException,
        InvalidKeyException, UnsupportedEncodingException, URISyntaxException {

    // Retrieve the proper URL components to sign
    String resource = path + '?' + query;

    // Get an HMAC-SHA1 signing key from the raw key bytes
    SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");

    // Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1 key
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(sha1Key);

    // compute the binary signature for the request
    byte[] sigBytes = mac.doFinal(resource.getBytes());

    // base 64 encode the binary signature
    String signature = Base64.encodeBytes(sigBytes);

    // convert the signature to 'web safe' base 64
    signature = signature.replace('+', '-');
    signature = signature.replace('/', '_');

    return resource + "&signature=" + signature;
}
 
開發者ID:blackarbiter,項目名稱:Android_Code_Arbiter,代碼行數:26,代碼來源:UrlSigner.java

示例2: 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:imliujun,項目名稱:LJFramework,代碼行數:24,代碼來源:EncryptUtils.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: hmac_sha

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * This method uses the JCE to provide the crypto algorithm.
 * HMAC computes a Hashed Message Authentication Code with the
 * crypto hash algorithm as a parameter.
 *
 * @param crypto   the crypto algorithm (HmacSHA1, HmacSHA256,
 *                 HmacSHA512)
 * @param keyBytes the bytes to use for the HMAC key
 * @param text     the message or text to be authenticated
 */
private static byte[] hmac_sha(String crypto, byte[] keyBytes,
                               byte[] text) {
    try {
        Mac hmac;
        hmac = Mac.getInstance(crypto);
        SecretKeySpec macKey =
                new SecretKeySpec(keyBytes, "RAW");
        hmac.init(macKey);
        return hmac.doFinal(text);
    } catch (GeneralSecurityException gse) {
        gse.printStackTrace();
        throw new UndeclaredThrowableException(gse);

    }
}
 
開發者ID:privacyidea,項目名稱:privacyidea-authenticator,代碼行數:26,代碼來源:OTPGenerator.java

示例5: computeAppSecretProof

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * The code in this method is based on this blog post: https://www.sammyk.me/the-single-most-important-way-to-make-your-facebook-app-more-secure
 * and this answer: https://stackoverflow.com/questions/7124735/hmac-sha256-algorithm-for-signature-calculation
 *
 * @param url the URL to which we're adding the proof
 * @param token the application token we pass back and forth
 * @param configuration the current configuration
 * @return URL with the appsecret_proof parameter added
 */
public String computeAppSecretProof(final String url, final OAuth2AccessToken token, final OAuth20Configuration configuration) {
    try {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(configuration.getSecret().getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        String proof = org.apache.commons.codec.binary.Hex.encodeHexString(sha256_HMAC.doFinal(token.getAccessToken().getBytes("UTF-8")));
        final String computedUrl = CommonHelper.addParameter(url, APPSECRET_PARAMETER, proof);
        return computedUrl;
    } catch (final Exception e) {
        throw new TechnicalException("Unable to compute appsecret_proof", e);
    }
}
 
開發者ID:millross,項目名稱:pac4j-async,代碼行數:22,代碼來源:FacebookProfileUrlCalculator.java

示例6: sign

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * @param stringData
 * @param key
 * @param algorithm
 * @return
 * @throws DapException 
 */
private static byte[] sign(String stringData, byte[] key, String algorithm) throws Exception {
    try {
        byte[] data = stringData.getBytes("UTF-8");
        Mac mac = Mac.getInstance(algorithm);
        mac.init(new SecretKeySpec(key, algorithm));
        return mac.doFinal(data);
    } catch (Exception e) {
        throw new Exception("Unable to calculate a request signature: " + e.getMessage());
    }
}
 
開發者ID:gkarthiks,項目名稱:s3-presigned-url,代碼行數:18,代碼來源:S3PresignedHttpUrlHelper.java

示例7: encode

import javax.crypto.Mac; //導入方法依賴的package包/類
private String encode(String key, String data) throws Exception {

		byte[] byteKey = key.getBytes("UTF-8");
		final String HMAC_SHA512 = "HmacSHA512";
		Mac sha512_HMAC = Mac.getInstance(HMAC_SHA512);
		SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512);
		sha512_HMAC.init(keySpec);
		byte[] mac_data = sha512_HMAC.doFinal(data.getBytes("UTF-8"));
		String result = bytesToHex(mac_data);
		return result;

	}
 
開發者ID:rgf2004,項目名稱:easypump,代碼行數:13,代碼來源:BittrexApi.java

示例8: getDigestedBytes

import javax.crypto.Mac; //導入方法依賴的package包/類
private byte[] getDigestedBytes(byte[] secretBytes, int iteration) {
  try {
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(new SecretKeySpec(secretBytes, "HmacSHA256"));
    return mac.doFinal(Conversions.intToByteArray(iteration));
  } catch (NoSuchAlgorithmException | java.security.InvalidKeyException e) {
    throw new AssertionError(e);
  }
}
 
開發者ID:XecureIT,項目名稱:PeSanKita-android,代碼行數:10,代碼來源:AsymmetricMasterCipher.java

示例9: encodeHmacSHA256

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * HmacSHA256加密
 * 
 * @param data 待加密數據
 * @param key 密鑰
 * @throws Exception
 */
public static byte[] encodeHmacSHA256(byte[] data, byte[] key) throws Exception {
	// 還原密鑰
	SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");
	// 實例化Mac
	Mac mac = Mac.getInstance(secretKey.getAlgorithm());
	// 初始化Mac
	mac.init(secretKey);
	// 執行消息摘要
	return mac.doFinal(data);
}
 
開發者ID:iBase4J,項目名稱:iBase4J-Common,代碼行數:18,代碼來源:HmacCoder.java

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

示例11: encodeHmacMD2

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * HmacMD2消息摘要
 * 
 * @param data 待做消息摘要處理的數據
 * @param byte[] 密鑰
 * @return byte[] 消息摘要
 * @throws Exception
 */
public static byte[] encodeHmacMD2(byte[] data, byte[] key) throws Exception {
	// 還原密鑰
	SecretKey secretKey = new SecretKeySpec(key, "HmacMD2");
	// 實例化Mac
	Mac mac = Mac.getInstance(secretKey.getAlgorithm());
	// 初始化Mac
	mac.init(secretKey);
	// 執行消息摘要
	return mac.doFinal(data);
}
 
開發者ID:babymm,項目名稱:mumu,代碼行數:19,代碼來源:HmacCoder.java

示例12: generate

import javax.crypto.Mac; //導入方法依賴的package包/類
public static int generate(byte[] key, long t, int digits)
{
    int r = 0;
    try {
        t /= 30;
        byte[] data = new byte[8];
        long value = t;
        for (int i = 8; i-- > 0; value >>>= 8) {
            data[i] = (byte) value;
        }

        SecretKeySpec signKey = new SecretKeySpec(key, SHA1);
        Mac mac = Mac.getInstance(SHA1);
        mac.init(signKey);
        byte[] hash = mac.doFinal(data);


        int offset = hash[20 - 1] & 0xF;

        long truncatedHash = 0;
        for (int i = 0; i < 4; ++i) {
            truncatedHash <<= 8;
            truncatedHash |= (hash[offset + i] & 0xFF);
        }

        truncatedHash &= 0x7FFFFFFF;
        truncatedHash %= Math.pow(10,digits);

        r  = (int) truncatedHash;
    }

    catch(Exception e){
    }

    return r;
}
 
開發者ID:nextcloud,項目名稱:passman-android,代碼行數:37,代碼來源:TOTPHelper.java

示例13: digestWithHmacSha1

import javax.crypto.Mac; //導入方法依賴的package包/類
public static byte[] digestWithHmacSha1(byte[] bArr, byte[] bArr2) {
    Mac instance;
    try {
        instance = Mac.getInstance("HmacSHA1");
    } catch (NoSuchAlgorithmException e) {
        instance = Mac.getInstance(HMAC_SHA_1);
    }
    instance.init(new SecretKeySpec(bArr2, RAW));
    return instance.doFinal(bArr);
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:11,代碼來源:CryptoUtil.java

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

示例15: 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:TIIEHenry,項目名稱:TIIEHenry-Android-SDK,代碼行數:21,代碼來源:EncryptUtils.java


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