List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:co.mitro.twofactor.TwoFactorCodeChecker.java
public static int computeHash(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; }/* ww w .ja v a 2s . c o m*/ 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; long truncatedHash = 0; for (int i = 0; i < 4; ++i) { truncatedHash <<= 8; truncatedHash |= (hash[offset + i] & 0xFF); } truncatedHash &= 0x7FFFFFFF; truncatedHash %= 1000000; return (int) truncatedHash; }
From source file:me.whitmarbut.mfa.TOTP.java
private byte[] getHmac(int timestamp, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec key_spec = new SecretKeySpec(key, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(key_spec);//from w w w. j av a 2s .co m byte[] bin_timestamp = ByteBuffer.allocate(4).putInt(timestamp).array(); ByteBuffer bbuff = ByteBuffer.allocate(8); bbuff.putInt(0); //Left pad 4 bytes to make a 64 bit int bbuff.putInt(timestamp); return mac.doFinal(bbuff.array()); }
From source file:com.jsmartframework.web.manager.CsrfEncrypter.java
private static Cipher getDecryptCipher(HttpServletRequest request, String key) throws Exception { Cipher decryptCipher = (Cipher) request.getAttribute(REQUEST_CSRF_DECRYPT_CIPHER); if (decryptCipher == null) { decryptCipher = Cipher.getInstance("AES"); SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES"); decryptCipher.init(Cipher.DECRYPT_MODE, secretKey); request.setAttribute(REQUEST_CSRF_DECRYPT_CIPHER, decryptCipher); }//from w ww . j ava 2 s . c o m return decryptCipher; }
From source file:at.alladin.rmbt.shared.Helperfunctions.java
public static String calculateHMAC(final String secret, final String data) { try {// ww w. j av a2 s. c om final SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), "HmacSHA1"); final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); final byte[] rawHmac = mac.doFinal(data.getBytes()); final String result = new String(Base64.encodeBytes(rawHmac)); return result; } catch (final GeneralSecurityException e) { System.out.println("Unexpected error while creating hash: " + e.getMessage()); return ""; } }
From source file:com.jsmartframework.web.manager.AuthEncrypter.java
private static Cipher getDecryptCipher(HttpServletRequest request, String key) throws Exception { Cipher decryptCipher = (Cipher) request.getAttribute(REQUEST_AUTH_DECRYPT_CIPHER); if (decryptCipher == null) { decryptCipher = Cipher.getInstance("AES"); SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES"); decryptCipher.init(Cipher.DECRYPT_MODE, secretKey); request.setAttribute(REQUEST_AUTH_DECRYPT_CIPHER, decryptCipher); }/* w ww .j a v a2 s. com*/ return decryptCipher; }
From source file:org.esupportail.papercut.services.HashService.java
public void setHmacKey(String hmacKey) { secretKey = new SecretKeySpec(DatatypeConverter.parseHexBinary(hmacKey), "HmacSHA512"); }
From source file:com.cloud.utils.EncryptionUtil.java
public static String generateSignature(String data, String key) { try {// ww w . j ava2s . 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:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java
public static String sign(byte[] auth_key, String message, byte hmac_type) throws NoSuchAlgorithmException, InvalidKeyException { // Check if hmac_type is valid if (hmac_type > 4 || hmac_type < 0) throw new IllegalArgumentException("Invalid digest type was specified"); // Create Mac instance Mac hmac;/*from w ww . j a va 2 s .c om*/ hmac = Mac.getInstance(HMAC_ALGORITHMS[hmac_type]); // Create key SecretKeySpec hmac_key = new SecretKeySpec(auth_key, HMAC_ALGORITHMS[hmac_type]); // Init hmac object hmac.init(hmac_key); // Prepare enc_part to calculate HMAC byte[] msg_to_hmac = FWKNOP_ENCRYPTION_HEADER.concat(message).getBytes(); // Calculate HMAC and return return message.concat(Base64.encodeBase64String(hmac.doFinal(msg_to_hmac)).replace("=", "")); }
From source file:com.imaginary.home.cloud.Configuration.java
static public @Nonnull String encrypt(@Nonnull String keySalt, @Nonnull String value) { try {//from w w w. j av a2 s.co m SecretKeySpec spec = new SecretKeySpec(getConfiguration().getCustomSalt(keySalt), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, spec); byte[] raw = value.getBytes("utf-8"); byte[] encrypted = cipher.doFinal(raw); byte[] b64 = Base64.encodeBase64(encrypted); return new String(b64, "utf-8"); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.scorpio4.util.io.IOStreamCrypto.java
public CipherOutputStream encrypt(OutputStream out) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { final SecretKey key = new SecretKeySpec(bytePassword, cipherSpec); final IvParameterSpec IV = new IvParameterSpec(ivBytes); final Cipher cipher = Cipher.getInstance(cipherTransformation); cipher.init(Cipher.ENCRYPT_MODE, key, IV); return new CipherOutputStream(new Base64OutputStream(out), cipher); }