Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ass.excercises;
- import java.security.NoSuchAlgorithmException;
- import java.security.spec.InvalidKeySpecException;
- import java.util.Base64;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.PBEKeySpec;
- public class HashingExample {
- public static void main(String[] args) {
- System.out.println(hashPassword("pepa", "IOI-655321", "PBKDF2WithHmacSHA1"));
- }
- public static String hashPassword(String password, String salt, String algorithm) {
- try {
- PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 128);
- SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
- SecretKey secret = factory.generateSecret(pbeKeySpec);
- Base64.Encoder enc = Base64.getEncoder();
- return enc.encodeToString(secret.getEncoded());
- } catch (NoSuchAlgorithmException e) {
- throw new IllegalArgumentException("Not a valid encryption algorithm", e);
- } catch (InvalidKeySpecException e) {
- throw new IllegalArgumentException("Not a valid secert key", e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement