1 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
這是 Bcrypt 庫始終需要格式化鹽。根據(jù)您的明文大小,如果小于 BCRYPT_SALT_LEN,rnd 的其余部分用隨機(jī)字節(jié)填充,其余部分與庫中一樣。
public static String gensalt(int log_rounds, SecureRandom random, String plaintextSalt) {
? ? byte[] plaintextByte = plaintextSalt.getBytes();
? ? byte rnd[] = new byte[BCRYPT_SALT_LEN];
? ? //Use all of the string if size >= of the reqired rnd size
? ? if (plaintextByte.length >= BCRYPT_SALT_LEN) {
? ? ? ? System.arraycopy(plaintextByte, 0, rnd, 0, rnd.length);
? ? } else {
? ? ? ? //copy all of the string byte array
? ? ? ? System.arraycopy(plaintextByte, 0, rnd, 0, plaintextByte.length);
? ? ? ? //fill the rest with random
? ? ? ? byte[] tempArray = new byte[BCRYPT_SALT_LEN - plaintextByte];
? ? ? ? random.nextBytes(tempArray);
? ? ? ? System.arraycopy(tempArray, 0, rnd, plaintextByte.length, temp.length);
? ? }
? ? StringBuffer rs = new StringBuffer();
? ? rs.append("$2a$");
? ? if (log_rounds < 10)
? ? ? ? rs.append("0");
? ? if (log_rounds > 30) {
? ? ? ? throw new IllegalArgumentException(
? ? ? ? ? ? ? ? "log_rounds exceeds maximum (30)");
? ? }
? ? rs.append(Integer.toString(log_rounds));
? ? rs.append("$");
? ? rs.append(encode_base64(rnd, rnd.length));
? ? return rs.toString();
}
添加回答
舉報(bào)