| 762 | } |
| 763 | |
| 764 | private static Pair<byte[], byte[]> getKeyPair(byte[] salt, String password) |
| 765 | throws NoSuchAlgorithmException, InvalidKeySpecException { |
| 766 | // https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html |
| 767 | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); |
| 768 | KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 310000, 2 * 256); |
| 769 | SecretKey secret = keyFactory.generateSecret(keySpec); |
| 770 | |
| 771 | byte[] encoded = secret.getEncoded(); |
| 772 | int half = encoded.length / 2; |
| 773 | return new Pair<>( |
| 774 | Arrays.copyOfRange(encoded, 0, half), |
| 775 | Arrays.copyOfRange(encoded, half, half + half)); |
| 776 | } |
| 777 | |
| 778 | private static byte[] getIv(long revision) { |
| 779 | byte[] iv = ByteBuffer.allocate(12) |