亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring security BCryptPasswordEncoder密碼驗(yàn)證原理詳解

瀏覽:75日期:2022-06-26 08:20:18

一、加密算法和hash算法的區(qū)別

加密算法是一種可逆的算法,基本過程就是對(duì)原來為明文的文件或數(shù)據(jù)按某種算法進(jìn)行處理,使其成為不可讀的一段代碼為“密文”,但在用相應(yīng)的密鑰進(jìn)行操作之后就可以得到原來的內(nèi)容 。

哈希算法是一種不可逆的算法,是把任意長(zhǎng)度的輸入通過散列算法變換成固定長(zhǎng)度的輸出,輸出就是散列值,不同的輸入可能會(huì)散列成相同的輸出,所以不可能從散列值來確定唯一的輸入值。

二、源碼解析

BCryptPasswordEncoder類實(shí)現(xiàn)了PasswordEncoder接口,這個(gè)接口中定義了兩個(gè)方法

public interface PasswordEncoder { String encode(CharSequence rawPassword); boolean matches(CharSequence rawPassword, String encodedPassword);}

其中encode(...)是對(duì)字符串進(jìn)行加密的方法,matches使用來校驗(yàn)傳入的明文密碼rawPassword是否和加密密碼encodedPassword相匹配的方法。即對(duì)密碼進(jìn)行加密時(shí)調(diào)用encode,登錄認(rèn)證時(shí)調(diào)用matches

下面我們來看下BCryptPasswordEncoder類中這兩個(gè)方法的具體實(shí)現(xiàn)

1. encode方法

public String encode(CharSequence rawPassword) { String salt; if (strength > 0) { if (random != null) { salt = BCrypt.gensalt(strength, random); } else { salt = BCrypt.gensalt(strength); } } else { salt = BCrypt.gensalt(); } return BCrypt.hashpw(rawPassword.toString(), salt);}

可以看到,這個(gè)方法中先基于某種規(guī)則得到了一個(gè)鹽值,然后在調(diào)用BCrypt.hashpw方法,傳入明文密碼和鹽值salt。所以我們?cè)倏聪翨Crypt.hashpw方法中做了什么

2. BCrypt.hashpw方法

public static String hashpw(String password, String salt) throws IllegalArgumentException { BCrypt B; String real_salt; byte passwordb[], saltb[], hashed[]; char minor = (char) 0; int rounds, off = 0; StringBuilder rs = new StringBuilder(); if (salt == null) { throw new IllegalArgumentException('salt cannot be null'); } int saltLength = salt.length(); if (saltLength < 28) { throw new IllegalArgumentException('Invalid salt'); } if (salt.charAt(0) != ’$’ || salt.charAt(1) != ’2’) { throw new IllegalArgumentException('Invalid salt version'); } if (salt.charAt(2) == ’$’) { off = 3; } else { minor = salt.charAt(2); if (minor != ’a’ || salt.charAt(3) != ’$’) {throw new IllegalArgumentException('Invalid salt revision'); } off = 4; } if (saltLength - off < 25) { throw new IllegalArgumentException('Invalid salt'); } // Extract number of rounds if (salt.charAt(off + 2) > ’$’) { throw new IllegalArgumentException('Missing salt rounds'); } rounds = Integer.parseInt(salt.substring(off, off + 2)); real_salt = salt.substring(off + 3, off + 25); try { passwordb = (password + (minor >= ’a’ ? '000' : '')).getBytes('UTF-8'); } catch (UnsupportedEncodingException uee) { throw new AssertionError('UTF-8 is not supported'); } saltb = decode_base64(real_salt, BCRYPT_SALT_LEN); B = new BCrypt(); hashed = B.crypt_raw(passwordb, saltb, rounds); rs.append('$2'); if (minor >= ’a’) { rs.append(minor); } rs.append('$'); if (rounds < 10) { rs.append('0'); } rs.append(rounds); rs.append('$'); encode_base64(saltb, saltb.length, rs); encode_base64(hashed, bf_crypt_ciphertext.length * 4 - 1, rs); return rs.toString(); }

可以看到,這個(gè)方法中先根據(jù)傳入的鹽值salt,然后基于某種規(guī)則從salt得到real_salt,后續(xù)的操作都是用這個(gè)real_salt來進(jìn)行,最終得到加密字符串。

所以這里有一個(gè)重點(diǎn):傳入的鹽值salt并不是最終用來加密的鹽,方法中通過salt得到了real_salt,記住這一點(diǎn),因?yàn)楹筮叺钠ヅ浞椒╩atches中要用到這一點(diǎn)。

3. matches方法

matches方法用來判斷一個(gè)明文是否和一個(gè)加密字符串對(duì)應(yīng)。

public boolean matches(CharSequence rawPassword, String encodedPassword) { if (encodedPassword == null || encodedPassword.length() == 0) { logger.warn('Empty encoded password'); return false; } if (!BCRYPT_PATTERN.matcher(encodedPassword).matches()) { logger.warn('Encoded password does not look like BCrypt'); return false; } return BCrypt.checkpw(rawPassword.toString(), encodedPassword);}

這個(gè)方法中先對(duì)密文字符串進(jìn)行了一些校驗(yàn),如果不符合規(guī)則直接返回不匹配,然后調(diào)用校驗(yàn)方法BCrypt.checkpw,第一個(gè)參數(shù)是明文,第二個(gè)參數(shù)是加密后的字符串。

public static boolean checkpw(String plaintext, String hashed) { return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));}static boolean equalsNoEarlyReturn(String a, String b) { char[] caa = a.toCharArray(); char[] cab = b.toCharArray(); if (caa.length != cab.length) { return false; } byte ret = 0; for (int i = 0; i < caa.length; i++) { ret |= caa[i] ^ cab[i]; } return ret == 0;}

注意 equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed))這里,第一個(gè)參數(shù)是加密后的字符串,而第二個(gè)參數(shù)是用剛才提過的hashpw方法對(duì)明文字符串進(jìn)行加密。

hashpw(plaintext, hashed)第一個(gè)參數(shù)是明文,第二個(gè)參數(shù)是加密字符串,但是在這里是作為鹽值salt傳入的,所以就用到了剛才說的 hashpw 內(nèi)部通過傳入的salt得到real_salt,這樣就保證了對(duì)現(xiàn)在要校驗(yàn)的明文的加密和得到已有密文的加密用的是同樣的加密策略,算法和鹽值都相同,這樣如果新產(chǎn)生的密文和原來的密文相同,則這兩個(gè)密文對(duì)應(yīng)的明文字符串就是相等的。

這也說明了加密時(shí)使用的鹽值被寫在了最終生成的加密字符串中。

三、總結(jié)

BCryptPasswordEncoder使用哈希算法+隨機(jī)鹽來對(duì)字符串加密。因?yàn)楣J且环N不可逆算法,所以密碼認(rèn)證時(shí)需要使用相同的算法+鹽值來對(duì)待校驗(yàn)的明文進(jìn)行加密,然后比較這兩個(gè)密文來進(jìn)行驗(yàn)證。BCryptPasswordEncoder在加密時(shí)通過從傳入的salt中獲取real_salt用來加密,保證了這一點(diǎn)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: word
相關(guān)文章:
主站蜘蛛池模板: 国产啪在线 | 久久色精品 | 青青热久久国产久精品秒播 | 一级视频免费观看 | 黄色片网站大全 | 韩国一级特黄清高免费大片 | 欧美一级特毛片 | 亚洲综合色丁香婷婷六月图片 | 538精品视频 | 可以直接看的毛片 | 一区二区三区四区在线播放 | 日韩成人免费aa在线看 | 无圣光私拍一区二区三区 | 亚洲综合视频 | 色在线免费 | 啪啪色视频| 日韩欧美亚洲一区 | 久久精品人人做人人 | 1024国产基地永久免费 | 国产大秀视频在线一区二区 | 最新亚洲人成网站在线影院 | 青草视频在线观看视频 | 中国一级黄| 国产在线视频色综合 | 做受又硬又粗又免费视频 | 亚洲七七久久综合桃花 | 国产又色又爽又黄的网站在线一级 | 日韩特级片 | 久久福利资源网站免费看 | 欧美亚洲中日韩中文字幕在线 | pans国产大尺度私密拍摄视频 | 国产精品自在线拍国产 | 激情网站网址 | 成人免费体验区福利云点播 | 免费羞羞视频网站 | 欧美一级久久久久久久大片 | 日韩一级免费视频 | 免费观看一级欧美大 | 全黄一级裸片视频免费 | 中文字幕国产综合 | 色婷婷国产 |