本文整理匯總了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);
}
}