Java Code Examples for javax.crypto.Mac#getAlgorithm()
The following examples show how to use
javax.crypto.Mac#getAlgorithm() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Sign.java From tencentcloud-sdk-java with Apache License 2.0 | 6 votes |
public static String sign(String secretKey, String sigStr, String sigMethod) throws TencentCloudSDKException { String sig = null; try { Mac mac = Mac.getInstance(sigMethod); byte[] hash; SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(UTF8), mac.getAlgorithm()); mac.init(secretKeySpec); hash = mac.doFinal(sigStr.getBytes(UTF8)); sig = DatatypeConverter.printBase64Binary(hash); } catch (Exception e) { throw new TencentCloudSDKException(e.getClass().getName() + "-" + e.getMessage()); } return sig; }
Example 2
Source File: CryptoUtilsImpl.java From zrtp-java with GNU Affero General Public License v3.0 | 5 votes |
private byte[] calculateHMAC(DigestType digestType, byte[] data, int offset, int length, byte[] aKey) { try { Mac mac = Mac.getInstance(digestType.getJCEHmacName()); SecretKeySpec key = new SecretKeySpec(aKey, mac.getAlgorithm()); mac.init(key); mac.update(data, offset, length); return mac.doFinal(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Failed to calc hmac " + digestType + e.getClass().getName() + ": " + e.getMessage()); } }
Example 3
Source File: HmacUtils.java From cxf with Apache License 2.0 | 5 votes |
public static byte[] computeHmac(byte[] key, Mac hmac, String data) { SecretKeySpec secretKey = new SecretKeySpec(key, hmac.getAlgorithm()); byte[] digest = computeHmac(secretKey, hmac, data); // Here we're finished with the SecretKey we created, so we can destroy it try { secretKey.destroy(); } catch (DestroyFailedException e) { // ignore } return digest; }
Example 4
Source File: HmacUtils.java From cxf with Apache License 2.0 | 5 votes |
public static byte[] computeHmac(byte[] key, String macAlgoJavaName, AlgorithmParameterSpec spec, String data) { Mac mac = getMac(macAlgoJavaName); SecretKeySpec secretKey = new SecretKeySpec(key, mac.getAlgorithm()); byte[] digest = computeHmac(secretKey, mac, spec, data); // Here we're finished with the SecretKey we created, so we can destroy it try { secretKey.destroy(); } catch (DestroyFailedException e) { LOG.log(Level.FINE, "Error destroying key: {}", e.getMessage()); } return digest; }
Example 5
Source File: SignUtil.java From java-sdk with Apache License 2.0 | 5 votes |
public static String hmacSha256(String key, String data) throws AipException { try { Mac mac = Mac.getInstance("HmacSHA256"); SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), mac.getAlgorithm()); mac.init(signingKey); return encodeHex(mac.doFinal(data.getBytes())); } catch (Exception e) { e.printStackTrace(); throw new AipException(-1, "Fail to generate HMAC-SHA256 signature"); } }
Example 6
Source File: MacUtil.java From Jose4j with Apache License 2.0 | 5 votes |
public static void initMacWithKey(Mac mac, Key key) throws org.jose4j.lang.InvalidKeyException { try { mac.init(key); } catch (InvalidKeyException e) { throw new org.jose4j.lang.InvalidKeyException("Key is not valid for " + mac.getAlgorithm(), e); } }
Example 7
Source File: PBKDF2KeyImpl.java From hottub with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 8
Source File: PBKDF2KeyImpl.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && Arrays.equals(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 9
Source File: PBKDF2KeyImpl.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && Arrays.equals(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 10
Source File: PBKDF2KeyImpl.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 11
Source File: PBKDF2KeyImpl.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase().hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && Arrays.equals(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 12
Source File: PBKDF2KeyImpl.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase().hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && Arrays.equals(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 13
Source File: PBKDF2KeyImpl.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 14
Source File: PBKDF2KeyImpl.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 15
Source File: PBKDF2KeyImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 16
Source File: PBKDF2KeyImpl.java From Bytecoder with Apache License 2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { @java.io.Serial private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys", gse); } return key; }
Example 17
Source File: PBKDF2KeyImpl.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 18
Source File: PBKDF2KeyImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 19
Source File: PBKDF2KeyImpl.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }
Example 20
Source File: PBKDF2KeyImpl.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
private static byte[] deriveKey(final Mac prf, final byte[] password, byte[] salt, int iterCount, int keyLengthInBit) { int keyLength = keyLengthInBit/8; byte[] key = new byte[keyLength]; try { int hlen = prf.getMacLength(); int intL = (keyLength + hlen - 1)/hlen; // ceiling int intR = keyLength - (intL - 1)*hlen; // residue byte[] ui = new byte[hlen]; byte[] ti = new byte[hlen]; // SecretKeySpec cannot be used, since password can be empty here. SecretKey macKey = new SecretKey() { private static final long serialVersionUID = 7874493593505141603L; @Override public String getAlgorithm() { return prf.getAlgorithm(); } @Override public String getFormat() { return "RAW"; } @Override public byte[] getEncoded() { return password; } @Override public int hashCode() { return Arrays.hashCode(password) * 41 + prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (this.getClass() != obj.getClass()) return false; SecretKey sk = (SecretKey)obj; return prf.getAlgorithm().equalsIgnoreCase( sk.getAlgorithm()) && MessageDigest.isEqual(password, sk.getEncoded()); } }; prf.init(macKey); byte[] ibytes = new byte[4]; for (int i = 1; i <= intL; i++) { prf.update(salt); ibytes[3] = (byte) i; ibytes[2] = (byte) ((i >> 8) & 0xff); ibytes[1] = (byte) ((i >> 16) & 0xff); ibytes[0] = (byte) ((i >> 24) & 0xff); prf.update(ibytes); prf.doFinal(ui, 0); System.arraycopy(ui, 0, ti, 0, ui.length); for (int j = 2; j <= iterCount; j++) { prf.update(ui); prf.doFinal(ui, 0); // XOR the intermediate Ui's together. for (int k = 0; k < ui.length; k++) { ti[k] ^= ui[k]; } } if (i == intL) { System.arraycopy(ti, 0, key, (i-1)*hlen, intR); } else { System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); } } } catch (GeneralSecurityException gse) { throw new RuntimeException("Error deriving PBKDF2 keys"); } return key; }