亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

可以在 BCrypt 中設置明文或將其轉換為鹽嗎?

可以在 BCrypt 中設置明文或將其轉換為鹽嗎?

qq_花開花謝_0 2023-06-21 13:57:02
我對密碼加密練習有一個要求,其中密碼的鹽是靜態的,并business_id+business_start_date根據客戶的企業 ID 和開始日期設置為 ( ) 值。在 BCrypt 文檔中,據說 BCrypt 在生成的哈希值中內置了鹽,以防止彩虹表攻擊。大多數示例使用 gensalt(int log_rounds) 函數。IMO,我肯定會像其他人一樣使用動態鹽,因為它更容易實現。但是,如果仍然堅持實施靜態鹽散列,是否有辦法讓 BCrypt 接受靜態散列或;如果不可能,我可以使用哪些其他加密來滿足該要求?該應用主要是80%的閱讀內容,少量的創建、更新、刪除操作。我剛剛做了一個測試,嘗試使用靜態鹽對密碼進行哈希處理。該方法在 BCrypt 實用程序類中使用:public static String hashPassWord(String textPassword, String salt){        String hashPassword = BCrypt.hashpw(textPassword, salt);        return hashPassword;}我正在測試的鹽是純文本格式的,例如(busId:3,businessDate:2019-02-04)String salt = new StringBuilder(busId).append(businessDate).toString();我也有這個方法作為備用,輪數(工作量)設置為10。public static String hashPassword(String textPassword){        String salt = BCrypt.gensalt(workload);        String hashPassword = BCrypt.hashpw(textPassword, salt);        return hashPassword;}當執行 hashpw() 函數時,無效的鹽版本錯誤會被拋出到異常中。
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

這是 Bcrypt 庫始終需要格式化鹽。根據您的明文大小,如果小于 BCRYPT_SALT_LEN,rnd 的其余部分用隨機字節填充,其余部分與庫中一樣。


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();


}


查看完整回答
反對 回復 2023-06-21
  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號