List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:com.iorga.iraj.security.SecurityUtils.java
public static String computeDataSignature(final String secretAccessKey, final String data) throws NoSuchAlgorithmException, InvalidKeyException { final SecretKeySpec secretKeySpec = new SecretKeySpec(secretAccessKey.getBytes(UTF8_CHARSET), "HmacSHA1"); final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKeySpec);/*from w ww .j av a 2 s.c o m*/ final String signature = Base64.encodeBase64String(mac.doFinal(data.getBytes(UTF8_CHARSET))); return StringUtils.chomp(signature); }
From source file:org.jahia.security.TOTP.java
private static int verify_code(byte[] key, long t) throws NoSuchAlgorithmException, InvalidKeyException { byte[] data = new byte[8]; long value = t; for (int i = 8; i-- > 0; value >>>= 8) { data[i] = (byte) value; }/* w w w. j a v a 2s . com*/ SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signKey); byte[] hash = mac.doFinal(data); int offset = hash[20 - 1] & 0xF; // We're using a long because Java hasn't got unsigned int. long truncatedHash = 0; for (int i = 0; i < 4; ++i) { truncatedHash <<= 8; // We are dealing with signed bytes: // we just keep the first byte. truncatedHash |= (hash[offset + i] & 0xFF); } truncatedHash &= 0x7FFFFFFF; truncatedHash %= 1000000; return (int) truncatedHash; }
From source file:com.amazonaws.cognito.devauthsample.Utilities.java
public static String sign(String content, String key) { try {/*from w ww . jav a 2 s .c o m*/ byte[] data = content.getBytes(ENCODING_FORMAT); Mac mac = Mac.getInstance(SIGNATURE_METHOD); mac.init(new SecretKeySpec(key.getBytes(ENCODING_FORMAT), SIGNATURE_METHOD)); char[] signature = Hex.encodeHex(mac.doFinal(data)); return new String(signature); } catch (Exception e) { log.log(Level.SEVERE, "Exception during sign", e); } return null; }
From source file:edu.internet2.middleware.openid.security.SecurityUtils.java
/** * Calculate signature for specified data using an Association. * //from ww w .j a va 2 s . c o m * @param association association * @param data data to calculate signature for * @return calculated signature * @throws SecurityException if unable to calculate the signature */ public static String calculateSignature(Association association, String data) throws SecurityException { log.debug("calculating signature using association: {}", association.getHandle()); log.debug("signature data = {}", data); try { Mac mac = Mac.getInstance(association.getMacKey().getAlgorithm()); mac.init(association.getMacKey()); byte[] rawHmac = mac.doFinal(data.getBytes()); return new String(Base64.encodeBase64(rawHmac)); } catch (InvalidKeyException e) { log.error("Unable to generate MAC - " + e.getMessage()); throw new SecurityException("Unable to generate MAC", e); } catch (NoSuchAlgorithmException e) { log.error("Unable to generate MAC - " + e.getMessage()); throw new SecurityException("Unable to generate MAC", e); } }
From source file:com.swdouglass.joid.Crypto.java
private static byte[] hmacShaX(String keySpec, byte[] key, byte[] text) throws InvalidKeyException, NoSuchAlgorithmException { SecretKey sk = new SecretKeySpec(key, keySpec); Mac m = Mac.getInstance(sk.getAlgorithm()); m.init(sk);//from w ww .j a va 2s . com return m.doFinal(text); }
From source file:mp3downloader.ZingSearch.java
private static String hash_hmac(String data, String key) { try {/*from www. ja v a 2 s . c o m*/ // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacMD5"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacMD5"); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String return new String(hexBytes, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.apache.nifi.toolkit.tls.util.TlsHelper.java
public static byte[] calculateHMac(String token, PublicKey publicKey) throws GeneralSecurityException { SecretKeySpec keySpec = new SecretKeySpec(token.getBytes(StandardCharsets.UTF_8), "RAW"); Mac mac = Mac.getInstance("Hmac-SHA256", BouncyCastleProvider.PROVIDER_NAME); mac.init(keySpec);//from w w w . j a v a 2 s . c o m return mac.doFinal(getKeyIdentifier(publicKey)); }
From source file:com.klinker.android.twitter.utils.api_helper.TwitterDMPicHelper.java
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException { SecretKey secretKey = null;//from w ww . j a v a 2 s .com byte[] keyBytes = keyString.getBytes(); secretKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] text = baseString.getBytes(); return new String(BASE64Encoder.encode(mac.doFinal(text))).trim(); }
From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java
public static boolean isValid(String plainText, String HMAC, String key) { try {//from www .ja v a2 s . c o m System.out.println("HMAC on = " + plainText); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256", new BouncyCastleProvider()); char password[] = key.toCharArray(); byte salt[] = "salt".getBytes(); KeySpec spec = new PBEKeySpec(password, salt, 65536, 256); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "HmacSHA256"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA256", new BouncyCastleProvider()); mac.init(secret); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(plainText.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String String check = new String(hexBytes, "UTF-8"); System.out.println("Checking = " + check); return check.equals(HMAC); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:chronic.app.ChronicCookie.java
public static String createAuthCode(byte[] secret, String string, long value) throws GeneralSecurityException { Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec signKey = new SecretKeySpec(secret, "HmacSHA1"); mac.init(signKey);//from ww w . j av a2 s.c o m mac.update(string.getBytes()); return new Base32().encodeAsString(mac.doFinal(Bytes.toByteArray(value))); }