Shiro密碼的MD5加密 MD5鹽值加密

2021-09-26 20:39:41 字數 2411 閱讀 2552

用md5加密演算法後,前台使用者輸入的字串如何使用md5加密,需要做的是將當前的realm 的credentialsmatcher屬性,替換為md5credentialsmatcher 由於md5credentialsmatcher已經過期了,推薦使用hashedcredentialsmatcher 並設定加密演算法即可。

原始碼

public ******hash(string algorithmname, object source, object  salt, int ha****erations)

throws codecexception, unknownalgorithmexception

this.algorithmname = algorithmname;

this.iterations = math.max(default_iterations, ha****erations);

bytesource saltbytes = null;

if (salt != null)

bytesource sourcebytes = convertsourcetobytes(source);

hash(sourcebytes, saltbytes, ha****erations);

}

測試 

public static void main(string args)
本來如果兩個密碼是相同的那麼產生的md5密碼也是一樣的,為了使兩個密碼相同的時候所產生的密碼還不一樣進一步提高安全性,所以考慮新增鹽值。使用以下進行新增鹽值:

bytesource credentialssalt = bytesource.util.bytes(username);

bytesource是個介面,介面裡邊有util的內部類,內部類的bytes方法

1). 在 dogetauthenticationinfo 方法返回值建立 ******authenticationinfo 物件的時候, 需要使用

******authenticationinfo(principal, credentials, credentialssalt, realmname) 構造器

2). 使用 bytesource.util.bytes() 來計算鹽值. 

3). 鹽值需要唯一: 一般使用隨機字串或 user id

4). 使用 new ******hash(hashalgorithmname, credentials, salt, ha****erations); 來計算鹽值加密後的密碼的值. 

例項**:

public class shirorealm extends authorizingrealm 

//5. 根據使用者資訊的情況, 決定是否需要丟擲其他的 authenticationexception 異常.

if("monster".equals(username))

//6. 根據使用者的情況, 來構建 authenticationinfo 物件並返回. 通常使用的實現類為: ******authenticationinfo

//以下資訊是從資料庫中獲取的.

//1). principal: 認證的實體資訊. 可以是 username, 也可以是資料表對應的使用者的實體類物件.

object principal = username;

//2). credentials: 密碼.

object credentials = null; //"fc1709d0a95a6be30bc5926fdb7f22f4";

if("admin".equals(username))else if("user".equals(username))

//3). realmname: 當前 realm 物件的 name. 呼叫父類的 getname() 方法即可

string realmname = getname();

//4). 鹽值.

bytesource credentialssalt = bytesource.util.bytes(username);

******authenticationinfo info = null; //new ******authenticationinfo(principal, credentials, realmname);

info = new ******authenticationinfo(principal, credentials, credentialssalt, realmname);

return info;

} //測試

public static void main(string args)

}

MD5 密碼加密

asp.net中md5加密碼的方法 page language c contenttype text html import namespace system asp.net中實現對密碼的加密 方法一 public static string encrypt string password,int ...

B S 密碼加密MD5初識

在此之前,小毛驢只聽說過密碼加密這件事情,簡單知道有明文儲存和對稱加密,那麼密碼加密還有哪些呢?盜取了一張圖 加密是一種演算法,通常我們使用的都是明文,直接把密碼新增到資料庫中,但是這樣對我們資料的安全是沒有任何防護的,需要用到密碼加密。它是一種單向hash演算法 雜湊演算法 保護密碼。先解釋一下h...

關於密碼進行MD5加密

system.security.cryptography.md5cryptoserviceprovider md5csp new system.security.cryptography.md5cryptoserviceprovider 例項化md5 獲取要加密的字段,並轉化為byte陣列 byte...