List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:org.dasein.cloud.cloudstack.CSMethod.java
private byte[] calculateHmac(String data, String key) throws SignatureException { try {/*from ww w. j a va2s .c om*/ SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); return mac.doFinal(data.getBytes()); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } }
From source file:com.sina.auth.AbstractAWSSigner.java
protected byte[] sign(byte[] data, byte[] key, SigningAlgorithm algorithm) throws SCSClientException { try {/*from w w w .j a v a 2s . c o m*/ Mac mac = Mac.getInstance(algorithm.toString()); mac.init(new SecretKeySpec(key, algorithm.toString())); return mac.doFinal(data); } catch (Exception e) { throw new SCSClientException("Unable to calculate a request signature: " + e.getMessage(), e); } }
From source file:org.apache.qpid.systest.rest.SaslRestTest.java
private byte[] generateCramMD5ClientResponse(String userName, String userPassword, byte[] challengeBytes) throws Exception { String macAlgorithm = "HmacMD5"; Mac mac = Mac.getInstance(macAlgorithm); mac.init(new SecretKeySpec(userPassword.getBytes("UTF-8"), macAlgorithm)); final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes); String responseAsString = userName + " " + toHex(messageAuthenticationCode); return responseAsString.getBytes(); }
From source file:org.apache.qpid.systest.rest.SaslRestTest.java
private byte[] generateCramMD5HexClientResponse(String userName, String userPassword, byte[] challengeBytes) throws Exception { String macAlgorithm = "HmacMD5"; byte[] digestedPasswordBytes = MessageDigest.getInstance("MD5").digest(userPassword.getBytes("UTF-8")); byte[] hexEncodedDigestedPasswordBytes = toHex(digestedPasswordBytes).getBytes("UTF-8"); Mac mac = Mac.getInstance(macAlgorithm); mac.init(new SecretKeySpec(hexEncodedDigestedPasswordBytes, macAlgorithm)); final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes); String responseAsString = userName + " " + toHex(messageAuthenticationCode); return responseAsString.getBytes(); }
From source file:com.leacox.pusher.Pusher.java
/** * Returns a HMAC/SHA256 representation of the given data. */// w w w. j a v a 2 s . c om private String hmacsha256Representation(String data) { try { // Create the HMAC/SHA256 key from application secret final SecretKeySpec signingKey = new SecretKeySpec(appSecret.getBytes(), "HmacSHA256"); // Create the message authentication code (MAC) final Mac mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); // Process and return data byte[] digest; // @TODO: decide if it's UTF-8 or not... digest = mac.doFinal(data.getBytes("UTF-8")); digest = mac.doFinal(data.getBytes()); // Convert to string BigInteger bigInteger = new BigInteger(1, digest); return String.format("%0" + (digest.length << 1) + "x", bigInteger); } catch (NoSuchAlgorithmException nsae) { // We should never come here, because GAE has HMac SHA256 throw new RuntimeException("No HMac SHA256 algorithm"); //} catch (UnsupportedEncodingException e) { // We should never come here, because UTF-8 should be available //throw new RuntimeException("No UTF-8"); } catch (InvalidKeyException e) { throw new RuntimeException("Invalid key exception while converting to HMac SHA256"); } }
From source file:net.mms_projects.copy_it.api.oauth.HeaderVerifier.java
/** * Validate the signature for the request, make sure you've called all verify* methods first * @param postRequestDecoder The post parameters for the request, pass null if it's a GET request instead * @param https Should we use https to generate our signature? * @throws OAuthException Thrown if the signature is invalid */// ww w .j av a 2 s . com public void checkSignature(HttpPostRequestDecoder postRequestDecoder, boolean https) throws UnsupportedEncodingException, URISyntaxException, OAuthException { final String signed_with = oauth_params.get(OAuthParameters.OAUTH_SIGNATURE); final String raw = createRaw(postRequestDecoder, https); final String secretkey = consumer.getSecretKey() + "&" + user.getSecretKey(); try { final Key signingKey = new SecretKeySpec(secretkey.getBytes(), HMAC_SHA1); final Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(signingKey); byte[] rawHmac = mac.doFinal(raw.getBytes()); final String signature = new String(Base64.encodeBase64(rawHmac)); System.err.println("Signed with: " + URLDecoder.decode(signed_with, UTF_8)); System.err.println("Should be::: " + signature); if (!URLDecoder.decode(signed_with, UTF_8).equals(signature)) throw new OAuthException(ErrorMessages.INVALID_SIGNATURE); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } }
From source file:n3phele.storage.swift.CloudStorageImpl.java
private final String signSwiftQueryString(String stringToSign, Credential credential) { try {//from ww w . j a v a 2 s . c om byte[] keyBytes = credential.decrypt().getSecret().getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); byte[] rawHmac = mac.doFinal(stringToSign.getBytes()); byte[] hexBytes = new Hex().encode(rawHmac); return new String(hexBytes, "UTF-8"); } catch (IllegalStateException e) { log.log(Level.SEVERE, "Signing error", e); throw new IllegalArgumentException(e.getMessage()); } catch (InvalidKeyException e) { log.log(Level.SEVERE, "Signing error", e); throw new IllegalArgumentException(e.getMessage()); } catch (NoSuchAlgorithmException e) { log.log(Level.SEVERE, "Signing error", e); throw new IllegalArgumentException(e.getMessage()); } catch (UnsupportedEncodingException e) { log.log(Level.SEVERE, "Signing error", e); throw new IllegalArgumentException(e.getMessage()); } }
From source file:com.axelor.apps.account.service.payment.PayboxService.java
/** * Fonction calculant la signature HMAC des paramtres * @param data/* w ww . j av a2 s.c om*/ * La chaine contenant les paramtres * @param hmacKey * La cl HMAC * @param algorithm * L'algorithme utilis (SHA512, ...) * @return * @throws AxelorException */ public String getHmacSignature(String data, String hmacKey, String algorithm) throws AxelorException { try { byte[] bytesKey = DatatypeConverter.parseHexBinary(hmacKey); SecretKeySpec secretKey = new SecretKeySpec(bytesKey, "Hmac" + algorithm); Mac mac = Mac.getInstance("Hmac" + algorithm); mac.init(secretKey); byte[] macData = mac.doFinal(data.getBytes(this.CHARSET)); // final byte[] hex = new Hex().encode( macData ); // return new String( hex, this.CHARSET ); // LOG.debug("Message HMAC 2 : {}",new String( hex, this.CHARSET )); String s = StringTool.getHexString(macData); return s.toUpperCase(); } catch (InvalidKeyException e) { throw new AxelorException(String.format("%s :\n %s", GeneralServiceImpl.EXCEPTION, e), IException.INCONSISTENCY); } catch (NoSuchAlgorithmException e) { throw new AxelorException(String.format("%s :\n %s", GeneralServiceImpl.EXCEPTION, e), IException.INCONSISTENCY); } catch (UnsupportedEncodingException e) { throw new AxelorException(String.format("%s :\n %s", GeneralServiceImpl.EXCEPTION, e), IException.INCONSISTENCY); } }
From source file:com.dongwookchung.nutritioncalculator.FatSecretAPI.java
/** * Returns signature generated using signature base as text and consumer secret as key * * @param method//from ww w. jav a 2s .c o m * Http method * @param uri * Request URL - http://platform.fatsecret.com/rest/server.api (Always remains the same) * @param params * An array of parameter values as "key=value" pair * * @return oauth_signature which will be added to request for calling fatsecret api */ public String sign(String method, String uri, String[] params) throws UnsupportedEncodingException { String encodedURI = encode(uri); String encodedParams = encode(paramify(params)); String[] p = { method, encodedURI, encodedParams }; String text = join(p, "&"); String key = APP_SECRET + "&"; SecretKey sk = new SecretKeySpec(key.getBytes(), APP_SIGNATURE_METHOD); String sign = ""; try { Mac m = Mac.getInstance(APP_SIGNATURE_METHOD); m.init(sk); sign = encode(new String(Base64.encode(m.doFinal(text.getBytes()), Base64.DEFAULT)).trim()); } catch (java.security.NoSuchAlgorithmException e) { } catch (java.security.InvalidKeyException e) { } return sign; }
From source file:com.dagobert_engine.core.service.MtGoxApiAdapter.java
/** * Signs a request with a secret//from w w w . j a v a2 s .co m * * @param secret * @param hash_data * @return */ private String signRequest(String secret, String hash_data) { String signature = ""; try { Mac mac = Mac.getInstance(Constants.SIGN_HASH_FUNCTION); SecretKeySpec secret_spec = new SecretKeySpec(Base64.decodeBase64(secret), Constants.SIGN_HASH_FUNCTION); mac.init(secret_spec); signature = Base64.encodeBase64String(mac.doFinal(hash_data.getBytes())); } catch (NoSuchAlgorithmException | InvalidKeyException e) { Logger.getLogger(MtGoxTradeService.class.getName()).log(Level.SEVERE, null, e); } return signature; }