本文整理汇总了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;
}
示例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;
}
}
示例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: 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);
}
}
示例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);
}
}
示例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());
}
}
示例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;
}
示例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);
}
}
示例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);
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
示例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);
}
示例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;
}
}