Skip to content

Instantly share code, notes, and snippets.

@Jaymo
Created February 29, 2016 08:21

Revisions

  1. Jaymo created this gist Feb 29, 2016.
    14 changes: 14 additions & 0 deletions generateSalt.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    public static final int SALT_LENGTH = 256;
    private static final String RANDOM_ALGORITHM = "SHA1PRNG";

    public String generateSalt() throws CryptoException {
    try {
    SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
    byte[] salt = new byte[SALT_LENGTH];
    random.nextBytes(salt);
    String saltHex = Util.HexEncoder.toHex(salt);
    return saltHex;
    } catch (Exception e) {
    throw new CryptoException("Unable to generate salt", e);
    }
    }