| 8 | import org.spongycastle.crypto.macs.HMac; |
| 9 | |
| 10 | public class Crypto { |
| 11 | public String pbkdf2(String secret, String salt, int iterations, int keyLength) { |
| 12 | try |
| 13 | { |
| 14 | PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest()); |
| 15 | byte[] secretData = secret.getBytes(); |
| 16 | byte[] saltData = salt.getBytes(); |
| 17 | gen.init(secretData, saltData, iterations); |
| 18 | byte[] derivedKey = ((KeyParameter)gen.generateDerivedParameters(keyLength * 8)).getKey(); |
| 19 | return toHex(derivedKey); |
| 20 | } |
| 21 | catch (Exception e){ |
| 22 | throw new RuntimeException(e); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | public String hmac(String key) { |
| 27 | try |
| 28 | { |
| 29 | HMac hmac = new HMac(new SHA256Digest()); |
| 30 | KeyParameter secret_key = new KeyParameter(key.getBytes()); |
| 31 | hmac.init(secret_key); |
| 32 | byte[] result = new byte[hmac.getMacSize()]; |
| 33 | hmac.doFinal(result, 0); |
| 34 | return toHex(result); |
| 35 | } |
| 36 | catch (Exception e){ |
| 37 | throw new RuntimeException(e); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | private static String toHex(byte[] bytes) { |
| 42 | BigInteger bi = new BigInteger(1, bytes); |
| 43 | return String.format("%0" + (bytes.length << 1) + "x", bi); |
| 44 | } |
| 45 | } |
nothing calls this directly
no outgoing calls
no test coverage detected