日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MD5散列算法

發布時間:2023/12/9 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MD5散列算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

散列算法:

通常需要對密碼進行散列,常用的有md5,sha
對md5密碼,如果知道散列后的值可以通過窮舉算法,得到md5密碼對應的明文。建議對md5進行散列時加salt(鹽),進行加密相當于對原始+鹽進行散列

正常使用時散列方法:

在程序中對原始密碼+鹽進行散列,將散列值存儲到數據庫中,并且還要講鹽也要存儲在數據庫中,如果進行密碼對比時,使用相同方法,將原始密碼+鹽進行散列,進行對比

散列方法測試

public static void main(String[] args) {//原始密碼String source = "111111";//鹽String salt = "wasd";//散列次數int hashIterations = 2;//散列一次:1b2814e7f4cbb32fea953252e45fface//散列兩次:174aec66f457f5169dbb1922393b6b4c//構造方法中://第一個參數:明文,原始密碼//第二個參數:鹽,通過使用隨機數//第三個參數:散列的次數,比如散列兩次,相當于md5(md5(''))Md5Hash md5Hash = new Md5Hash(source, salt, hashIterations);String password_md5 = md5Hash.toString();System.out.println(password_md5);//第一個參數:散列算法SimpleHash simpleHash = new SimpleHash("md5", source, salt, hashIterations);System.out.println(simpleHash.toString());}

ini配置文件整合和為讀取數據庫

[main] #定義憑證匹配器 credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher #散列算法 credentialsMatcher.hashAlgorithmName=md5 #散列次數 credentialsMatcher.hashIterations=2#將憑證匹配器設置到realm customRealm=cn.dinggc.shiro.realm.CustomRealMd5 customRealm.credentialsMatcher=$credentialsMatcher securityManager.realms=$customRealm

自定義realmmd5

public class CustomRealMd5 extends AuthorizingRealm{//設置realm的名稱@Overridepublic void setName(String name) {super.setName("customRealmMd5");}//用于認證@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {// TODO Auto-generated method stub//token是用戶輸入的//第一步從token中取出身份信息String userCode = (String)token.getPrincipal();//第二步:根據用戶輸入的userCode從數據庫查詢//...//模擬從數據庫查詢密碼,散列值String password = "174aec66f457f5169dbb1922393b6b4c";//從數據庫中獲取saltString salit = "wasd";//如果查詢不到返回null//數據庫中用戶賬號是zhangsan/*if(!userCode.equals("zhangsansan")) {return null;}*///如果查詢到返回認證信息AuthenticationInfoSimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userCode, password,ByteSource.Util.bytes(salit),this.getName());return simpleAuthenticationInfo;}//用于授權@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {// TODO Auto-generated method stubreturn null;} }

md5測試

// 用戶登錄和退出@Testpublic void testCustomRealmMd5() {// 創建securityManager工廠,用過ini配置文件創建securityManager工廠Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm-md5.ini");// 創建SecurityManagerSecurityManager securityManager = factory.getInstance();// 將securityManager設置到當前的運行環境中SecurityUtils.setSecurityManager(securityManager);// 從SecurityUtils里邊創建一個subjectSubject subject = SecurityUtils.getSubject();// 在認證提交前準備token(令牌)UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111111");try {// 執行認證提交subject.login(token);} catch (AuthenticationException e) {// TODO Auto-generated catch blocke.printStackTrace();}//是否認證通過boolean isAuthenticated = subject.isAuthenticated();System.out.println("是否認證通過: " + isAuthenticated);//退出操作subject.logout();//是否認證通過isAuthenticated = subject.isAuthenticated();System.out.println("是否認證通過: " + isAuthenticated);}

總結

以上是生活随笔為你收集整理的MD5散列算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。