List of usage examples for javax.crypto Mac getInstance
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.rsmart.rfabric.jasperreports.auth.Signature.java
private void instatiateMac() { try {/* w w w . j a va2 s. c o m*/ // Get an hmac_sha1 Mac instance mac = Mac.getInstance(hmacSha1Algorithm); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } }
From source file:com.kolich.aws.signing.impl.KolichAwsSigner.java
@Override public final String sign(final AwsCredentials credentials, final String input) { try {//from w ww. j a va2s .com final String algoName = algorithm_.toString(); // Get a new instance of the HMAC-SHA1 algorithm. final Mac mac = Mac.getInstance(algoName); // Init it with our secret and the secret-key algorithm. mac.init(new SecretKeySpec(credentials.getSecretBytes(), algoName)); // Sign the input. return encodeBase64ToString(mac.doFinal(getBytesUtf8(input))); } catch (Exception e) { throw new KolichAwsException( "Failed to sign input " + "string (algorithm=" + algorithm_ + ", input=" + input + ")", e); } }
From source file:com.spartan.springmvc.bean.FacebookSignatureBean.java
/** * Parses and verifies a Facebook signed_request parameter. The data of the signed request is returned on successful verification. * * @param signedRequest// www . j a v a 2 s .c o m * @param appSecret * @return * @return * @throws FacebookSignatureVerificationFailedException */ @SuppressWarnings("unchecked") public <T> T parseSignature(String signedRequest, String appSecret, Class<T> clazz) throws FacebookSignatureVerificationFailedException { String[] parts = signedRequest.split("\\."); if (parts.length != 2) { throw new FacebookSignatureVerificationFailedException("Invalid signature format."); } String encSig = parts[0]; String encPayload = parts[1]; Base64 decoder = new Base64(true); try { Mac mac = Mac.getInstance("HMACSHA256"); mac.init(new SecretKeySpec(appSecret.getBytes(), mac.getAlgorithm())); byte[] calcSig = mac.doFinal(encPayload.getBytes()); byte[] decodedSig = decoder.decode(encSig); boolean isVerified = Arrays.equals(decodedSig, calcSig); if (isVerified) { try { String unsignedData = new String(decoder.decode(encPayload)); logger.debug("unsignedData: " + unsignedData); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); T data = mapper.readValue(unsignedData, (Class<T>) clazz); return data; } catch (IOException e) { throw new FacebookSignatureVerificationFailedException( "Failed to parse JSON data: " + e.getMessage(), e); } } else { throw new FacebookSignatureVerificationFailedException("Signatures do not match."); } } catch (NoSuchAlgorithmException e) { throw new FacebookSignatureVerificationFailedException(e); } catch (InvalidKeyException e) { throw new FacebookSignatureVerificationFailedException(e); } }
From source file:org.alfresco.encryption.MACUtils.java
protected Mac getMac(String keyAlias) throws Exception { Mac mac = threadMac.get();/*w ww .j ava2 s . c o m*/ if (mac == null) { mac = Mac.getInstance(macAlgorithm); threadMac.set(mac); } Key key = keyProvider.getKey(keyAlias); if (key == null) { throw new AlfrescoRuntimeException("Unexpected null key for key alias " + keyAlias); } mac.init(key); return mac; }
From source file:com.parworks.androidlibrary.utils.HMacShaPasswordEncoder.java
protected final Mac getMac() throws IllegalArgumentException { try {/*from www . j ava2 s . c o m*/ return Mac.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("No such algorithm [" + algorithm + "]"); } }
From source file:com.google.walkaround.util.server.auth.DigestUtils2.java
/** * Computes the RFC2104 SHA1 HMAC digest for the given message and secret. * * @param message the data to compute the digest of *//* w w w. ja v a 2 s .c o m*/ public static byte[] sha1hmac(Secret key, byte[] message) { try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.data, HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes byte[] ret = mac.doFinal(message); assert ret.length == SHA1_BLOCK_SIZE; return ret; } catch (InvalidKeyException e) { throw new RuntimeException("Failed to generate HMAC", e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Failed to generate HMAC", e); } }
From source file:com.google.resting.rest.util.oauth.SignatureUtil.java
/** * Sign request./*from w ww. j ava 2s .co m*/ * * @param keyString Consumer key for request signing. * @param targetDomain Domain of the REST endpoint (Ex. login.yahoo.com) * @param verb Type of REST operation (GET/POST/PUT/DELETE) * @param isSecureInvocation HTTP/HTTPS * @param contextPathElement Path element in the base REST uri (Ex. /weather/india) * @param inputParams Collection of request params for REST request (Ex. city=calcutta ) * @return * @throws NoSuchAlgorithmException The exception is thrown if the encryption algorithm is not supported. * @throws InvalidKeyException The exception is thrown if the consumer key is invalid * @throws IllegalStateException * @throws UnsupportedEncodingException The exception is thrown if the URL encoding is incorrect. */ public static String getSignature(String keyString, String targetDomain, Verb verb, boolean isSecureInvocation, String contextPathElement, List<NameValuePair> inputParams, String messageEncoding) throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException, UnsupportedEncodingException { String baseString = getBaseString(targetDomain, verb.toString(), isSecureInvocation, contextPathElement, inputParams, messageEncoding).replace("+", "%20").replace("*", "%2A").replace("%7E", "~"); System.out.println("Base string is " + baseString); Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec secret = new SecretKeySpec(keyString.getBytes(messageEncoding), mac.getAlgorithm()); mac.init(secret); byte[] digest = mac.doFinal(baseString.getBytes(messageEncoding)); return new String(Base64.encodeBase64(digest)).replace(RequestConstants.CARRIAGE_RETURN, RequestConstants.EMPTY_STRING); }
From source file:fr.ortolang.diffusion.security.authentication.TOTPHelper.java
private static byte[] hmacSha(byte[] keyBytes, byte[] text) { try {//from ww w . j av a 2 s . c o m Mac hmac; hmac = Mac.getInstance("HmacSHA1"); SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW"); hmac.init(macKey); return hmac.doFinal(text); } catch (GeneralSecurityException gse) { throw new UndeclaredThrowableException(gse); } }
From source file:net.jingx.main.Main.java
public static String computePin(String secret, Long counter) { if (secret == null || secret.length() == 0) { return "Null or empty secret"; }/*from ww w. j ava2 s . c om*/ try { final byte[] keyBytes = Base32String.decode(secret); Mac mac = Mac.getInstance("HMACSHA1"); mac.init(new SecretKeySpec(keyBytes, "")); PasscodeGenerator pcg = new PasscodeGenerator(mac); if (counter == null) { // time-based totp return pcg.generateTimeoutCode(); } else { // counter-based hotp return pcg.generateResponseCode(counter); } } catch (GeneralSecurityException e) { return "General security exception"; } catch (DecodingException e) { return "Decoding exception"; } }
From source file:org.animotron.bridge.Principal.java
private Mac getMac() { try {/*from w ww . j a va2s . c o m*/ Mac mac = Mac.getInstance("HmacSHA1"); mac.init(keySpec); return mac; } catch (Exception e) { e.printStackTrace(); } return null; }