List of usage examples for javax.crypto Mac getInstance
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.java
/** * Creates response for the second step of the SRP authentication. * * @param challenge REQUIRED: {@link InitiateAuthResult} contains next * challenge./* w ww. j a v a 2s . c o m*/ * @param authenticationDetails REQUIRED: {@link AuthenticationDetails} user * authentication details. * @param authenticationHelper REQUIRED: Internal helper class for SRP * calculations. * @return {@link RespondToAuthChallengeRequest}. */ private RespondToAuthChallengeRequest userSrpAuthRequest(InitiateAuthResult challenge, AuthenticationDetails authenticationDetails, AuthenticationHelper authenticationHelper) { final String userIdForSRP = challenge.getChallengeParameters() .get(CognitoServiceConstants.CHLG_PARAM_USER_ID_FOR_SRP); this.usernameInternal = challenge.getChallengeParameters().get(CognitoServiceConstants.CHLG_PARAM_USERNAME); this.deviceKey = CognitoDeviceHelper.getDeviceKey(usernameInternal, pool.getUserPoolId(), context); secretHash = CognitoSecretHash.getSecretHash(usernameInternal, clientId, clientSecret); final BigInteger srpB = new BigInteger(challenge.getChallengeParameters().get("SRP_B"), 16); if (srpB.mod(AuthenticationHelper.N).equals(BigInteger.ZERO)) { throw new CognitoInternalErrorException("SRP error, B cannot be zero"); } final BigInteger salt = new BigInteger(challenge.getChallengeParameters().get("SALT"), 16); final byte[] key = authenticationHelper.getPasswordAuthenticationKey(userIdForSRP, authenticationDetails.getPassword(), srpB, salt); final Date timestamp = new Date(); byte[] hmac; String dateString; try { final Mac mac = Mac.getInstance("HmacSHA256"); final SecretKeySpec keySpec = new SecretKeySpec(key, "HmacSHA256"); mac.init(keySpec); mac.update(pool.getUserPoolId().split("_", 2)[1].getBytes(StringUtils.UTF8)); mac.update(userIdForSRP.getBytes(StringUtils.UTF8)); final byte[] secretBlock = Base64.decode(challenge.getChallengeParameters().get("SECRET_BLOCK")); mac.update(secretBlock); final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US); simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); dateString = simpleDateFormat.format(timestamp); final byte[] dateBytes = dateString.getBytes(StringUtils.UTF8); hmac = mac.doFinal(dateBytes); } catch (final Exception e) { throw new CognitoInternalErrorException("SRP error", e); } final Map<String, String> srpAuthResponses = new HashMap<String, String>(); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_PASSWORD_CLAIM_SECRET_BLOCK, challenge.getChallengeParameters().get(CognitoServiceConstants.CHLG_PARAM_SECRET_BLOCK)); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_PASSWORD_CLAIM_SIGNATURE, new String(Base64.encode(hmac), StringUtils.UTF8)); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_TIMESTAMP, dateString); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_USERNAME, usernameInternal); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_DEVICE_KEY, deviceKey); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_SECRET_HASH, secretHash); final RespondToAuthChallengeRequest authChallengeRequest = new RespondToAuthChallengeRequest(); authChallengeRequest.setChallengeName(challenge.getChallengeName()); authChallengeRequest.setClientId(clientId); authChallengeRequest.setSession(challenge.getSession()); authChallengeRequest.setChallengeResponses(srpAuthResponses); return authChallengeRequest; }
From source file:com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.java
/** * Creates request for device SRP verification. * * @param challenge REQUIRED: {@link RespondToAuthChallengeResult} contains * next challenge.//from ww w . ja va 2 s .c o m * @param deviceSecret REQUIRED: Device secret verifier. * @param authenticationHelper REQUIRED: Internal helper class for SRP * calculations. * @param deviceGroupKey the device group key * @return {@link RespondToAuthChallengeRequest}. */ public RespondToAuthChallengeRequest deviceSrpAuthRequest(RespondToAuthChallengeResult challenge, String deviceSecret, String deviceGroupKey, AuthenticationHelper authenticationHelper) { this.usernameInternal = challenge.getChallengeParameters().get(CognitoServiceConstants.CHLG_PARAM_USERNAME); final BigInteger srpB = new BigInteger(challenge.getChallengeParameters().get("SRP_B"), 16); if (srpB.mod(AuthenticationHelper.N).equals(BigInteger.ZERO)) { throw new CognitoInternalErrorException("SRP error, B cannot be zero"); } final BigInteger salt = new BigInteger(challenge.getChallengeParameters().get("SALT"), 16); final byte[] key = authenticationHelper.getPasswordAuthenticationKey(deviceKey, deviceSecret, srpB, salt); final Date timestamp = new Date(); byte[] hmac; String dateString; try { final Mac mac = Mac.getInstance("HmacSHA256"); final SecretKeySpec keySpec = new SecretKeySpec(key, "HmacSHA256"); mac.init(keySpec); mac.update(deviceGroupKey.getBytes(StringUtils.UTF8)); mac.update(deviceKey.getBytes(StringUtils.UTF8)); final byte[] secretBlock = Base64.decode( challenge.getChallengeParameters().get(CognitoServiceConstants.CHLG_PARAM_SECRET_BLOCK)); mac.update(secretBlock); final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US); simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); dateString = simpleDateFormat.format(timestamp); final byte[] dateBytes = dateString.getBytes(StringUtils.UTF8); hmac = mac.doFinal(dateBytes); } catch (final Exception e) { throw new CognitoInternalErrorException("SRP error", e); } secretHash = CognitoSecretHash.getSecretHash(usernameInternal, clientId, clientSecret); final Map<String, String> srpAuthResponses = new HashMap<String, String>(); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_PASSWORD_CLAIM_SECRET_BLOCK, challenge.getChallengeParameters().get(CognitoServiceConstants.CHLG_PARAM_SECRET_BLOCK)); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_PASSWORD_CLAIM_SIGNATURE, new String(Base64.encode(hmac), StringUtils.UTF8)); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_TIMESTAMP, dateString); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_USERNAME, usernameInternal); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_DEVICE_KEY, deviceKey); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_SECRET_HASH, secretHash); final RespondToAuthChallengeRequest authChallengeRequest = new RespondToAuthChallengeRequest(); authChallengeRequest.setChallengeName(challenge.getChallengeName()); authChallengeRequest.setClientId(clientId); authChallengeRequest.setSession(challenge.getSession()); authChallengeRequest.setChallengeResponses(srpAuthResponses); return authChallengeRequest; }
From source file:API.amazon.mws.feeds.service.MarketplaceWebServiceClient.java
/** * Computes RFC 2104-compliant HMAC signature. * *//*from ww w . j a v a 2 s.c om*/ private String sign(String data, String key, String algorithm) throws SignatureException { byte[] signature; try { Mac mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(key.getBytes(), algorithm)); signature = Base64.encodeBase64(mac.doFinal(data.getBytes(DEFAULT_ENCODING))); } catch (Exception e) { throw new SignatureException("Failed to generate signature: " + e.getMessage(), e); } return new String(signature); }
From source file:com.cloud.test.stress.TestClientWithAPI.java
public static String signRequest(String request, String key) { try {//from w w w. j av a 2s. com Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1"); mac.init(keySpec); mac.update(request.getBytes()); byte[] encryptedBytes = mac.doFinal(); return org.apache.commons.codec.binary.Base64.encodeBase64String(encryptedBytes); } catch (Exception ex) { s_logger.error("unable to sign request", ex); } return null; }
From source file:com.cloud.server.ManagementServerImpl.java
private String signRequest(String request, String key) { try {//from w w w . j a v a 2 s. c om s_logger.info("Request: " + request); s_logger.info("Key: " + key); if (key != null && request != null) { Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1"); mac.init(keySpec); mac.update(request.getBytes()); byte[] encryptedBytes = mac.doFinal(); return new String((Base64.encodeBase64(encryptedBytes))); } } catch (Exception ex) { s_logger.error("unable to sign request", ex); } return null; }
From source file:carnero.cgeo.original.libs.Base.java
public static byte[] hashHmac(String text, String salt) { byte[] macBytes = {}; try {// www . j a va2 s . co m SecretKeySpec secretKeySpec = new SecretKeySpec(salt.getBytes(), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKeySpec); macBytes = mac.doFinal(text.getBytes()); } catch (Exception e) { Log.e(Settings.tag, "cgBase.hashHmac: " + e.toString()); } return macBytes; }
From source file:carnero.cgeo.cgBase.java
public static byte[] hashHmac(String text, String salt) { byte[] macBytes = {}; try {/*from w ww . jav a 2 s . c o m*/ SecretKeySpec secretKeySpec = new SecretKeySpec(salt.getBytes(), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKeySpec); macBytes = mac.doFinal(text.getBytes()); } catch (Exception e) { Log.e(cgSettings.tag, "cgBase.hashHmac: " + e.toString()); } return macBytes; }