本文整理汇总了Java中javax.crypto.Mac.getInstance方法的典型用法代码示例。如果您正苦于以下问题:Java Mac.getInstance方法的具体用法?Java Mac.getInstance怎么用?Java Mac.getInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.Mac
的用法示例。
在下文中一共展示了Mac.getInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: hashCredentials
import javax.crypto.Mac; //导入方法依赖的package包/类
/**
* Hash user name and password according to the algorithm required by the Miniserver
*
* @param hashKeyHex
* string with hash key received from the Miniserver in hex characters
* @return
* hashed credentials to send to the Miniserver for authentication
*/
private String hashCredentials(String hashKeyHex) {
if (user == null || password == null || hashKeyHex == null) {
return null;
}
try {
byte[] hashKeyBytes = Hex.decodeHex(hashKeyHex.toCharArray());
SecretKeySpec signKey = new SecretKeySpec(hashKeyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signKey);
String data = user + ":" + password;
byte[] rawData = mac.doFinal(data.getBytes());
byte[] hexData = new Hex().encode(rawData);
return new String(hexData, "UTF-8");
} catch (DecoderException | NoSuchAlgorithmException | InvalidKeyException
| UnsupportedEncodingException e) {
logger.debug("[{}] Error encrypting credentials: {}", debugId, e.getMessage());
return null;
}
}
示例2: 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()));
}
示例3: if
import javax.crypto.Mac; //导入方法依赖的package包/类
private byte[] b0449щ0449щ04490449(String str, byte[] bArr) throws Exception {
try {
String str2 = "HmacSHA256";
if (((bХ0425ХХХХ + bХХ0425ХХХ) * bХ0425ХХХХ) % bХ042504250425ХХ != b04250425ХХХХ) {
bХ0425ХХХХ = 57;
b04250425ХХХХ = bХ04250425ХХХ();
}
try {
Mac instance = Mac.getInstance(str2);
instance.init(new SecretKeySpec(bArr, str2));
return instance.doFinal(str.getBytes(UrlUtils.UTF8));
} catch (Exception e) {
throw e;
}
} catch (Exception e2) {
throw e2;
}
}
示例4: hmacSHA256
import javax.crypto.Mac; //导入方法依赖的package包/类
static byte[] hmacSHA256(String data, byte[] key) {
try {
String algorithm = "HmacSHA256";
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(data.getBytes(UTF_8));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例5: encodeHmacSHA256
import javax.crypto.Mac; //导入方法依赖的package包/类
/**
* HmacSHA256加密
*
* @param data 待加密数据
* @param key 密钥
* @return byte[] 消息摘要
* @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);
}
示例6: generateHash
import javax.crypto.Mac; //导入方法依赖的package包/类
/**
* Generate a Hmac SHA-256 hash
* @param key key
* @param string value
* @return hashed
*/
public static String generateHash(String key, String string) {
SecretKeySpec object = new SecretKeySpec(key.getBytes(), "HmacSHA256");
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init((Key) object);
byte[] byteArray = mac.doFinal(string.getBytes("UTF-8"));
return new String(new Hex().encode(byteArray), "ISO-8859-1");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例7: calculateSignature
import javax.crypto.Mac; //导入方法依赖的package包/类
public static String calculateSignature(int nonce, long timestamp) {
String data = composeParametersToString(nonce, timestamp);
try {
SecretKeySpec signingKey = new SecretKeySpec(ApiConstants.AUTH_SECRET.getBytes(),
HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return toHexString(mac.doFinal(data.getBytes()));
} catch(NoSuchAlgorithmException | InvalidKeyException ex){
Logger.logExceptionToFabric(ex);
}
return null;
}
示例8: 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;
}
示例9: encodeHmacSHA
import javax.crypto.Mac; //导入方法依赖的package包/类
/**
* HmacSHA1加密
*
* @param data 待加密数据
* @param key 密钥
* @return byte[] 消息摘要
* @throws Exception
*/
public static byte[] encodeHmacSHA(byte[] data, byte[] key) throws Exception {
// 还原密钥
SecretKey secretKey = new SecretKeySpec(key, "HMacTiger");
// 实例化Mac SslMacMD5
Mac mac = Mac.getInstance("SslMacMD5");// secretKey.getAlgorithm());
// 初始化Mac
mac.init(secretKey);
// 执行消息摘要
return mac.doFinal(data);
}
示例10: 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);
}
示例11: MasterCipher
import javax.crypto.Mac; //导入方法依赖的package包/类
public MasterCipher(MasterSecret masterSecret) {
try {
this.masterSecret = masterSecret;
this.encryptingCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
this.decryptingCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
this.hmac = Mac.getInstance("HmacSHA1");
} catch (NoSuchPaddingException | NoSuchAlgorithmException nspe) {
throw new AssertionError(nspe);
}
}
示例12: doTest
import javax.crypto.Mac; //导入方法依赖的package包/类
@Override
public void doTest(String alg) throws NoSuchAlgorithmException,
InvalidKeyException, NoSuchProviderException {
SecretKey key = Utils.getSecretKeySpec();
// instantiate Mac object and init it with a SecretKey
Mac mac = Mac.getInstance(alg, "SunJCE");
mac.init(key);
// prepare buffer
byte[] data = new byte[BUFFER_SIZE];
for (int i = 0; i < BUFFER_SIZE; i++) {
data[i] = (byte) (i % 256);
}
ByteBuffer buf = ByteBuffer.wrap(data);
int limitBefore = buf.limit();
mac.update(buf);
mac.doFinal();
int limitAfter = buf.limit();
int positonAfter = buf.position();
if (limitAfter != limitBefore) {
System.out.println("limit after = " + limitAfter);
System.out.println("limit before = " + limitBefore);
throw new RuntimeException("Test failed: "
+ "limit of buffer has been chenged.");
}
if (positonAfter != limitAfter) {
System.out.println("position after = " + positonAfter);
System.out.println("limit after = " + limitAfter);
throw new RuntimeException("Test failed: "
+ "position of buffer isn't equal to its limit");
}
}
示例13: sign
import javax.crypto.Mac; //导入方法依赖的package包/类
private String sign(
String httpVerb,
String contentMD5,
String contentType,
String date,
String resource,
Map<String, String> metas) {
StringBuilder stringToSign =
new StringBuilder(
httpVerb
+ "\n"
+ CharMatcher.whitespace().trimFrom(contentMD5)
+ "\n"
+ CharMatcher.whitespace().trimFrom(contentType)
+ "\n"
+ date
+ "\n");
if (metas != null) {
for (Map.Entry<String, String> entity : metas.entrySet()) {
stringToSign
.append(CharMatcher.whitespace().trimFrom(entity.getKey()))
.append(":")
.append(CharMatcher.whitespace().trimFrom(entity.getValue()))
.append("\n");
}
}
stringToSign.append(resource);
try {
Mac mac = Mac.getInstance("HmacSHA1");
byte[] keyBytes = secretKey.getBytes("UTF8");
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
mac.init(signingKey);
byte[] signBytes = mac.doFinal(stringToSign.toString().getBytes("UTF8"));
String signature = encodeBase64(signBytes);
return "AWS" + " " + accessKey + ":" + signature;
} catch (Exception e) {
throw new RuntimeException("MAC CALC FAILED.");
}
}
示例14: hmacSha1
import javax.crypto.Mac; //导入方法依赖的package包/类
/**
* 使用HMAC-SHA1进行消息签名, 返回字节数组,长度为20字节.
*
* @param input 原始输入字符数组
* @param key HMAC-SHA1密钥
*/
public static byte[] hmacSha1(byte[] input, byte[] key) {
try {
SecretKey secretKey = new SecretKeySpec(key, HMACSHA1);
Mac mac = Mac.getInstance(HMACSHA1);
mac.init(secretKey);
return mac.doFinal(input);
} catch (GeneralSecurityException e) {
throw Exceptions.unchecked(e);
}
}
示例15: IntegrityHmac
import javax.crypto.Mac; //导入方法依赖的package包/类
/**
* Method IntegrityHmac
*
* @throws XMLSignatureException
*/
public IntegrityHmac() throws XMLSignatureException {
String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "Created IntegrityHmacSHA1 using " + algorithmID);
}
try {
this.macAlgorithm = Mac.getInstance(algorithmID);
} catch (java.security.NoSuchAlgorithmException ex) {
Object[] exArgs = { algorithmID, ex.getLocalizedMessage() };
throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
}
}