Generate a salt for use with the BCrypt.hashpw() method @param prefix the prefix value (default $2a) @param log_rounds the log2 of the number of rounds of hashing to apply - the work factor therefore increases as 2 log_rounds. @param random an instance of SecureRandom to use @return an encoded sa
(String prefix, int log_rounds, SecureRandom random)
| 816 | * @exception IllegalArgumentException if prefix or log_rounds is invalid |
| 817 | */ |
| 818 | public static String gensalt(String prefix, int log_rounds, SecureRandom random) |
| 819 | throws IllegalArgumentException { |
| 820 | StringBuilder rs = new StringBuilder(); |
| 821 | byte rnd[] = new byte[BCRYPT_SALT_LEN]; |
| 822 | |
| 823 | if (!prefix.startsWith("$2") || |
| 824 | (prefix.charAt(2) != 'a' && prefix.charAt(2) != 'y' && |
| 825 | prefix.charAt(2) != 'b')) { |
| 826 | throw new IllegalArgumentException ("Invalid prefix"); |
| 827 | } |
| 828 | if (log_rounds < 4 || log_rounds > 31) { |
| 829 | throw new IllegalArgumentException ("Invalid log_rounds"); |
| 830 | } |
| 831 | |
| 832 | random.nextBytes(rnd); |
| 833 | |
| 834 | rs.append("$2"); |
| 835 | rs.append(prefix.charAt(2)); |
| 836 | rs.append("$"); |
| 837 | if (log_rounds < 10) |
| 838 | rs.append("0"); |
| 839 | rs.append(log_rounds); |
| 840 | rs.append("$"); |
| 841 | encode_base64(rnd, rnd.length, rs); |
| 842 | return rs.toString(); |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Generate a salt for use with the BCrypt.hashpw() method |