List of usage examples for javax.crypto Mac getAlgorithm
public final String getAlgorithm()
From source file:com.skplanet.syruppay.token.tav.TransactionAuthenticationValue.java
public boolean isValidBy(final String key, final String checksum) throws NoSuchAlgorithmException, IOException, InvalidKeyException { final Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(key.getBytes("UTF-8"), mac.getAlgorithm())); mac.update((cardToken + mctTransAuthId + ocTransAuthId + new ObjectMapper().writeValueAsString(paymentAuthenticationDetail)).getBytes("UTF-8")); return Base64.encodeBase64URLSafeString(mac.doFinal()).equals(checksum); }
From source file:org.opencredo.cloud.storage.azure.rest.internal.RequestAuthorizationInterceptor.java
/** * @param signatureString// w ww . j a va2 s . c o m * @return */ private String createSignature(String signatureString) throws RequestAuthorizationException { String encoding = "UTF-8"; String encryptionAlgorithm = "HmacSHA256"; try { Mac mac = Mac.getInstance(encryptionAlgorithm); mac.init(new SecretKeySpec(Base64.decodeBase64(credentials.getSecretKey()), mac.getAlgorithm())); byte[] dataToMAC = signatureString.getBytes(encoding); mac.update(dataToMAC); byte[] result = mac.doFinal(); return new String(Base64.encodeBase64(result)); } catch (InvalidKeyException e) { throw new RequestAuthorizationException( "Provided secret key is inappropriate to encrypt signature-string.", e); } catch (NoSuchAlgorithmException e) { throw new RequestAuthorizationException( "No algorithm [" + encryptionAlgorithm + "] to encrypt signature-string.", e); } catch (UnsupportedEncodingException e) { throw new RequestAuthorizationException( "Unable to convert signature-string to encoding - '" + encoding + "'.", e); } catch (IllegalStateException e) { throw new RequestAuthorizationException("Illegal signature-string encryption state.", e); } }
From source file:org.picketbox.json.enc.JSONWebEncryption.java
private byte[] performMac(byte[] key, byte[] data) throws ProcessingException { Mac mac = null; try {/* ww w . j av a 2 s .c o m*/ mac = Mac.getInstance(jsonWebEncryptionHeader.getMessageAuthenticationCodeAlgo()); mac.init(new SecretKeySpec(key, mac.getAlgorithm())); mac.update(data); return mac.doFinal(); } catch (Exception e) { throw PicketBoxJSONMessages.MESSAGES.processingException(e); } }
From source file:org.soyatec.windowsazure.authenticate.SharedKeyCredentials.java
private String computeMacSha(String canonicalizedString) { Mac mac; try {//from w w w.j a v a2s.c om if (getKey() == null) { throw new StorageClientException(StorageErrorCode.AccountNotFound, "The Windows Azure storage account credentials contains invalid values.", HttpStatusConstant.DEFAULT_STATUS, null, null); } mac = Mac.getInstance(HMACSHA256); mac.init(new SecretKeySpec(getKey(), mac.getAlgorithm())); byte[] dataToMAC = canonicalizedString.getBytes(UTF8_CHARSET); mac.update(dataToMAC); byte[] result = mac.doFinal(); return Base64.encode(result); } catch (NoSuchAlgorithmException e) { Logger.error("NoSuchAlgorithmException", e); } catch (InvalidKeyException e) { Logger.error("InvalidKeyException", e); } catch (UnsupportedEncodingException e) { Logger.error("UnsupportedEncodingException", e); } return null; }
From source file:com.microsoft.windowsazure.messaging.Connection.java
/** * Generates an AuthToken/* w ww. j a v a2 s.c om*/ * @param url The target URL * @return An AuthToken * @throws java.security.InvalidKeyException */ private String generateAuthToken(String url) throws InvalidKeyException { String keyName = mConnectionData.get(SHARED_ACCESS_KEY_NAME); if (isNullOrWhiteSpace(keyName)) { throw new AssertionError("SharedAccessKeyName"); } String key = mConnectionData.get(SHARED_ACCESS_KEY); if (isNullOrWhiteSpace(key)) { throw new AssertionError("SharedAccessKey"); } try { url = URLEncoder.encode(url, UTF8_ENCODING).toLowerCase(Locale.ENGLISH); } catch (UnsupportedEncodingException e) { // this shouldn't happen because of the fixed encoding } // Set expiration in seconds Calendar expireDate = Calendar.getInstance(TimeZone.getTimeZone(UTC_TIME_ZONE)); expireDate.add(Calendar.MINUTE, EXPIRE_MINUTES); long expires = expireDate.getTimeInMillis() / 1000; String toSign = url + '\n' + expires; // sign byte[] bytesToSign = toSign.getBytes(); Mac mac = null; try { mac = Mac.getInstance("HmacSHA256"); } catch (NoSuchAlgorithmException e) { // This shouldn't happen because of the fixed algorithm } SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm()); mac.init(secret); byte[] signedHash = mac.doFinal(bytesToSign); String base64Signature = Base64.encodeToString(signedHash, Base64.DEFAULT); base64Signature = base64Signature.trim(); try { base64Signature = URLEncoder.encode(base64Signature, UTF8_ENCODING); } catch (UnsupportedEncodingException e) { // this shouldn't happen because of the fixed encoding } // construct authorization string String token = "SharedAccessSignature sr=" + url + "&sig=" + base64Signature + "&se=" + expires + "&skn=" + keyName; return token; }