Skip to content

Instantly share code, notes, and snippets.

@erancihan
Created November 19, 2019 09:15
Show Gist options
  • Save erancihan/ea31f918061231e7a1675bdfd570a3f5 to your computer and use it in GitHub Desktop.
Save erancihan/ea31f918061231e7a1675bdfd570a3f5 to your computer and use it in GitHub Desktop.
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Base64;
public class Salter {
public static void main(String[] args)
{
System.out.println(">: " + salt("password", "[email protected]"));
}
public static String salt(String A, String B)
{
byte[] hash = new byte[0];
try {
KeySpec spec = new PBEKeySpec(
A.toCharArray(),
B.getBytes(StandardCharsets.UTF_8),
65536,
128
);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
hash = factory.generateSecret(spec).getEncoded();
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
}
return Base64.getEncoder().encodeToString(hash);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment