List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal() throws IllegalStateException
From source file:MainClass.java
public static void main(String args[]) throws Exception { SecretKeySpec k = new SecretKeySpec("1234".getBytes(), "HMACSHA1"); Mac m = Mac.getInstance("HmacMD5"); m.init(k);/* www. j a v a2s . c o m*/ m.update("test".getBytes("UTF8")); byte s[] = m.doFinal(); for (int i = 0; i < s.length; i++) { System.out.print(Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6)); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { SecureRandom random = new SecureRandom(); byte[] keyBytes = new byte[20]; random.nextBytes(keyBytes);//from w w w . ja v a2 s . co m SecretKeySpec key = new SecretKeySpec(keyBytes, "HMACSHA1"); System.out.println("Key:" + new BASE64Encoder().encode(key.getEncoded())); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(key); mac.update("test".getBytes("UTF8")); byte[] result = mac.doFinal(); System.out.println("MAC: " + new BASE64Encoder().encode(result)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String alg = "HmacMD5"; Mac mac = Mac.getInstance(alg); KeyGenerator kg = KeyGenerator.getInstance(alg); SecretKey key = kg.generateKey(); mac.init(key);//from ww w .j a v a2s.c om mac.update("test".getBytes()); byte[] b = mac.doFinal(); System.out.println(new String(b)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); SecureRandom random = new SecureRandom(); IvParameterSpec ivSpec = createCtrIvForAES(); Key key = createKeyForAES(256, random); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); String input = "12345678"; Mac mac = Mac.getInstance("DES", "BC"); byte[] macKeyBytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; Key macKey = new SecretKeySpec(macKeyBytes, "DES"); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] cipherText = new byte[cipher.getOutputSize(input.length() + mac.getMacLength())]; int ctLength = cipher.update(input.getBytes(), 0, input.length(), cipherText, 0); mac.init(macKey);/*from ww w . j a v a 2 s .com*/ mac.update(input.getBytes()); ctLength += cipher.doFinal(mac.doFinal(), 0, mac.getMacLength(), cipherText, ctLength); cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] plainText = cipher.doFinal(cipherText, 0, ctLength); int messageLength = plainText.length - mac.getMacLength(); mac.init(macKey); mac.update(plainText, 0, messageLength); byte[] messageHash = new byte[mac.getMacLength()]; System.arraycopy(plainText, messageLength, messageHash, 0, messageHash.length); System.out.println("plain : " + new String(plainText) + " verified: " + MessageDigest.isEqual(mac.doFinal(), messageHash)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); SecureRandom random = new SecureRandom(); IvParameterSpec ivSpec = createCtrIvForAES(1, random); Key key = createKeyForAES(256, random); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); String input = "www.java2s.com"; Mac mac = Mac.getInstance("DES", "BC"); byte[] macKeyBytes = "12345678".getBytes(); Key macKey = new SecretKeySpec(macKeyBytes, "DES"); System.out.println("input : " + input); // encryption step cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] cipherText = new byte[cipher.getOutputSize(input.length() + mac.getMacLength())]; int ctLength = cipher.update(input.getBytes(), 0, input.length(), cipherText, 0); mac.init(macKey);/*from ww w . j a va 2 s. co m*/ mac.update(input.getBytes()); ctLength += cipher.doFinal(mac.doFinal(), 0, mac.getMacLength(), cipherText, ctLength); System.out.println("cipherText : " + new String(cipherText)); // decryption step cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] plainText = cipher.doFinal(cipherText, 0, ctLength); int messageLength = plainText.length - mac.getMacLength(); mac.init(macKey); mac.update(plainText, 0, messageLength); byte[] messageHash = new byte[mac.getMacLength()]; System.arraycopy(plainText, messageLength, messageHash, 0, messageHash.length); System.out.println("plain : " + new String(plainText) + " verified: " + MessageDigest.isEqual(mac.doFinal(), messageHash)); }
From source file:com.cloud.utils.EncryptionUtil.java
public static String generateSignature(String data, String key) { try {/*from w w w .j a v a2 s. c o m*/ final Mac mac = Mac.getInstance("HmacSHA1"); final SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1"); mac.init(keySpec); mac.update(data.getBytes("UTF-8")); final byte[] encryptedBytes = mac.doFinal(); return Base64.encodeBase64String(encryptedBytes); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { s_logger.error("exception occurred which encoding the data." + e.getMessage()); throw new CloudRuntimeException("unable to generate signature", e); } }
From source file:com.cloud.sample.UserCloudAPIExecutor.java
/** * 1. Signs a string with a secret key using SHA-1 2. Base64 encode the result 3. URL encode the final result * /* ww w .ja v a 2 s . c o m*/ * @param request * @param key * @return */ public static String signRequest(String request, String key) { try { Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1"); mac.init(keySpec); mac.update(request.getBytes()); byte[] encryptedBytes = mac.doFinal(); return URLEncoder.encode(Base64.encodeBase64String(encryptedBytes), "UTF-8"); } catch (Exception ex) { System.out.println(ex); } return null; }
From source file:ch.cyberduck.core.sftp.openssh.OpenSSHHostKeyVerifier.java
private static byte[] hmacSha1Hash(byte[] salt, String hostname) throws IOException { try {/* w w w . ja v a 2 s . c om*/ final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(salt, 0, salt.length, mac.getAlgorithm())); mac.update(hostname.getBytes()); return mac.doFinal(); } catch (GeneralSecurityException e) { throw new IOException(e); } }
From source file:org.identityconnectors.office365.jsontoken.JWTTokenHelper.java
/** * Sign the text with the symmetric key. * @param signingKey The Signing Key./* w ww. j av a 2s . c o m*/ * @param rawToken The rawToken that needs to be signed. * @return Signed byte array. * @throws SampleAppException */ private static byte[] signData(String signingKey, String rawToken) throws Exception { SecretKeySpec secretKey = null; secretKey = new SecretKeySpec(com.sun.org.apache.xerces.internal.impl.dv.util.Base64.decode(signingKey), "HmacSHA256"); Mac mac; byte[] signedData = null; mac = Mac.getInstance("HmacSHA256"); mac.init(secretKey); mac.update(rawToken.getBytes("UTF-8")); signedData = mac.doFinal(); return signedData; }
From source file:org.apache.shindig.common.crypto.Crypto.java
/** * HMAC sha1//from w w w. j a va 2s .c om * * @param key the key must be at least 8 bytes in length. * @param in byte array to HMAC. * @return the hash * * @throws GeneralSecurityException */ public static byte[] hmacSha1(byte[] key, byte[] in) throws GeneralSecurityException { if (key.length < MIN_HMAC_KEY_LEN) { throw new GeneralSecurityException("HMAC key should be at least " + MIN_HMAC_KEY_LEN + " bytes."); } Mac hmac = Mac.getInstance(HMAC_TYPE); Key hmacKey = new SecretKeySpec(key, HMAC_TYPE); hmac.init(hmacKey); hmac.update(in); return hmac.doFinal(); }