List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:oauth.signpost.signature.HmacSha1MessageSigner.java
@Override public String sign(HttpRequest request, Map<String, String> oauthParameters) throws OAuthMessageSignerException { try {/*from w ww.j av a 2 s .c o m*/ String keyString = OAuth.percentEncode(getConsumerSecret()) + '&' + OAuth.percentEncode(getTokenSecret()); byte[] keyBytes = keyString.getBytes(OAuth.ENCODING); SecretKey key = new SecretKeySpec(keyBytes, MAC_NAME); Mac mac = Mac.getInstance(MAC_NAME); mac.init(key); String sbs = computeSignatureBaseString(request, oauthParameters); byte[] text = sbs.getBytes(OAuth.ENCODING); return base64Encode(mac.doFinal(text)); } catch (GeneralSecurityException e) { throw new OAuthMessageSignerException(e); } catch (UnsupportedEncodingException e) { throw new OAuthMessageSignerException(e); } }
From source file:edu.wfu.inotado.helper.EncryptionHelper.java
public String calculateHMAC(String data, String key) throws java.security.SignatureException { String result = ""; try {// w w w. j a va 2 s . c o m if (!StringUtils.isBlank(data) && !StringUtils.isBlank(key)) { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), 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[] rawHmac = mac.doFinal(data.getBytes()); // base64-encode the hmac result = new String(Base64.encodeBase64(rawHmac)); } else { log.warn("data or key appears to be empty!"); } } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }
From source file:net.alegen.datpass.library.crypto.CryptoManager.java
public String generateHmac(EncryptionOutput encryptionOutput, String password) { final String ctext = encryptionOutput.getCypherText(); final String salt = encryptionOutput.getSalt(); final String iv = encryptionOutput.getIV(); try {/*from w w w . j a va 2s .c o m*/ // create the hmac key and message MessageDigest md = MessageDigest.getInstance("SHA-512"); byte[] hmacKey = md.digest(password.getBytes("UTF-8")); byte[] hmacMessage = (iv + ctext + salt).getBytes("UTF-8"); // generate hmac SecretKeySpec keySpec = new SecretKeySpec(hmacKey, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(keySpec); byte[] result = mac.doFinal(hmacMessage); // return result result = Base64.encodeBase64(result); return new String(result, "UTF-8"); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { log.error("An error occured while generating an HMAC value."); return null; } }
From source file:com.chbase.android.simplexml.SodaAuthenticator.java
/** * Sign content.//from w w w . j ava 2 s.c om * * @param content the content * * @return the byte[] */ private byte[] signContent(byte[] content) { try { SecretKeySpec sks = new SecretKeySpec(authenticationSecret, "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(sks); return mac.doFinal(content); } catch (Exception e) { throw new HVSystemException("Could not sign request", e); } }
From source file:com.eucalyptus.objectstorage.pipeline.WalrusSoapUserAuthenticationHandler.java
protected String checkSignature(final String queryKey, final String subject) throws AuthenticationException { SecretKeySpec signingKey = new SecretKeySpec(queryKey.getBytes(), Hmac.HmacSHA1.toString()); try {/*from w w w . j av a 2s . c o m*/ Mac mac = Hmac.HmacSHA1.getInstance(); mac.init(signingKey); byte[] rawHmac = mac.doFinal(subject.getBytes()); return new String(Base64.encode(rawHmac)).replaceAll("=", ""); } catch (Exception e) { LOG.error(e, e); throw new AuthenticationException("Failed to compute signature"); } }
From source file:com.linecorp.platform.channel.sample.BusinessConnect.java
public boolean validateBCRequest(String httpRequestBody, String channelSecret, String channelSignature) { if (httpRequestBody == null || channelSecret == null || channelSignature == null) { return false; }/*from ww w. j av a 2 s . c o m*/ String signature; SecretKeySpec key = new SecretKeySpec(channelSecret.getBytes(), "HmacSHA256"); try { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(key); byte[] source = httpRequestBody.getBytes("UTF-8"); signature = Base64.encodeBase64String(mac.doFinal(source)); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { e.printStackTrace(); return false; } return channelSignature.equals(signature); }
From source file:org.cryptomator.crypto.engine.impl.CryptorImpl.java
@Override public byte[] writeKeysToMasterkeyFile(CharSequence passphrase) { final byte[] scryptSalt = new byte[SCRYPT_SALT_LENGTH]; randomSource.nextBytes(scryptSalt);/*from w w w . ja v a 2s.c om*/ final byte[] kekBytes = Scrypt.scrypt(passphrase, scryptSalt, SCRYPT_COST_PARAM, SCRYPT_BLOCK_SIZE, KEYLENGTH_IN_BYTES); final byte[] wrappedEncryptionKey; final byte[] wrappedMacKey; try { final SecretKey kek = new SecretKeySpec(kekBytes, ENCRYPTION_ALG); wrappedEncryptionKey = AesKeyWrap.wrap(kek, encryptionKey); wrappedMacKey = AesKeyWrap.wrap(kek, macKey); } finally { Arrays.fill(kekBytes, (byte) 0x00); } final Mac mac = new ThreadLocalMac(macKey, MAC_ALG).get(); final byte[] versionMac = mac .doFinal(ByteBuffer.allocate(Integer.BYTES).putInt(CURRENT_VAULT_VERSION).array()); final KeyFile keyfile = new KeyFile(); keyfile.setVersion(CURRENT_VAULT_VERSION); keyfile.setScryptSalt(scryptSalt); keyfile.setScryptCostParam(SCRYPT_COST_PARAM); keyfile.setScryptBlockSize(SCRYPT_BLOCK_SIZE); keyfile.setEncryptionMasterKey(wrappedEncryptionKey); keyfile.setMacMasterKey(wrappedMacKey); keyfile.setVersionMac(versionMac); try { final ObjectMapper om = new ObjectMapper(); return om.writeValueAsBytes(keyfile); } catch (JsonProcessingException e) { throw new IllegalArgumentException("Unable to create JSON from " + keyfile, e); } }
From source file:fr.Axeldu18.PterodactylAPI.PterodactylAPI.java
public String hmac(String url) throws Exception { try {/*from w w w . j ava 2 s .co m*/ Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256"); sha256_HMAC.init(secret_key); String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(url.getBytes())); return hash; } catch (Exception e) { log(Level.SEVERE, " HMAC Error"); return null; } }
From source file:com.microsoft.azure.batch.auth.BatchCredentialsInterceptor.java
private String sign(String accessKey, String stringToSign) { try {//from ww w. j av a 2s . com // Encoding the Signature // Signature=Base64(HMAC-SHA256(UTF8(StringToSign))) Mac hmac = Mac.getInstance("hmacSHA256"); hmac.init(new SecretKeySpec(Base64.decodeBase64(accessKey), "hmacSHA256")); byte[] digest = hmac.doFinal(stringToSign.getBytes("UTF-8")); return Base64.encodeBase64String(digest); } catch (Exception e) { throw new IllegalArgumentException("accessKey", e); } }
From source file:org.apache.nifi.web.security.otp.OtpService.java
/** * Hashes the specified authentication token. The resulting value will be used as the one time use token. * * @param authenticationToken the authentication token * @return the one time use token *///w ww . j a va2 s . co m private String hash(final OtpAuthenticationToken authenticationToken) { try { // input is the user identity and timestamp final String input = authenticationToken.getName() + "-" + System.nanoTime(); // create the secret using secure random final SecureRandom secureRandom = new SecureRandom(); final byte[] randomBytes = new byte[32]; secureRandom.nextBytes(randomBytes); final SecretKeySpec secret = new SecretKeySpec(randomBytes, HMAC_SHA256); // 256 bit // hash the input final Mac hmacSha256 = Mac.getInstance(HMAC_SHA256); hmacSha256.init(secret); final byte[] output = hmacSha256.doFinal(input.getBytes(StandardCharsets.UTF_8)); // return the result as a base 64 string return Base64.encodeBase64URLSafeString(output); } catch (final NoSuchAlgorithmException | InvalidKeyException e) { final String errorMessage = "There was an error generating the OTP"; logger.error(errorMessage, e); throw new IllegalStateException("Unable to generate single use token."); } }