Shiro 的CryptFormat源码
来源:互联网 发布:海文考研网络课程 编辑:程序博客网 时间:2024/06/05 05:09
package org.apache.shiro.crypto.hash.format;import org.apache.shiro.codec.Base64;import org.apache.shiro.crypto.hash.Hash;import org.apache.shiro.crypto.hash.SimpleHash;import org.apache.shiro.util.ByteSource;import org.apache.shiro.util.StringUtils;public class Shiro1CryptFormat implements ModularCryptFormat, ParsableHashFormat { public static final String ID = "shiro1"; public static final String MCF_PREFIX = TOKEN_DELIMITER + ID + TOKEN_DELIMITER; public Shiro1CryptFormat() { } public String getId() { return ID; } public String format(Hash hash) { if (hash == null) { return null; } String algorithmName = hash.getAlgorithmName(); ByteSource salt = hash.getSalt(); int iterations = hash.getIterations(); StringBuilder sb = new StringBuilder(MCF_PREFIX).append(algorithmName).append(TOKEN_DELIMITER).append(iterations).append(TOKEN_DELIMITER); if (salt != null) { sb.append(salt.toBase64()); } sb.append(TOKEN_DELIMITER); sb.append(hash.toBase64()); return sb.toString(); } public Hash parse(String formatted) { if (formatted == null) { return null; } if (!formatted.startsWith(MCF_PREFIX)) { //TODO create a HashFormatException class String msg = "The argument is not a valid '" + ID + "' formatted hash."; throw new IllegalArgumentException(msg); } String suffix = formatted.substring(MCF_PREFIX.length()); String[] parts = suffix.split("\\$"); //last part is always the digest/checksum, Base64-encoded: int i = parts.length-1; String digestBase64 = parts[i--]; //second-to-last part is always the salt, Base64-encoded: String saltBase64 = parts[i--]; String iterationsString = parts[i--]; String algorithmName = parts[i]; byte[] digest = Base64.decode(digestBase64); ByteSource salt = null; if (StringUtils.hasLength(saltBase64)) { byte[] saltBytes = Base64.decode(saltBase64); salt = ByteSource.Util.bytes(saltBytes); } int iterations; try { iterations = Integer.parseInt(iterationsString); } catch (NumberFormatException e) { String msg = "Unable to parse formatted hash string: " + formatted; throw new IllegalArgumentException(msg, e); } SimpleHash hash = new SimpleHash(algorithmName); hash.setBytes(digest); if (salt != null) { hash.setSalt(salt); } hash.setIterations(iterations); return hash; }}
0 0
- Shiro 的CryptFormat源码
- Shiro笔记(二)----shiro源码与默认提供的示例
- Shiro的Filter机制详解---源码分析
- 安全认证框架Shiro(三)- 源码角度解析shiro的权限验证
- Shiro源码学习之一
- Shiro源码分析
- Shiro源码之SecurityManager
- shiro加盐走源码
- shiro认证源码
- shiro授权源码
- Shiro源码分析之两种Session的方式
- 我的shiro之旅: 十八 例子源码
- Shiro安全框架的登陆认证源码解析
- Shiro源码研究之处理一次完整的请求
- shiro-03shiro的filterChainDefinitions
- shiro-04shiro的缓存
- Shiro源码分析-初始化-Realm
- Shiro源码学习之ShiroFilter
- 【黑马程序员】SQLserver笔记之存储过程
- C语言常用字符串函数的实现二
- 职场人 你迷茫了吗?
- 自定义Spinner(下拉列表)五步走
- Uni2D 入门+Uni2D 入门 -- Atlas
- Shiro 的CryptFormat源码
- QoS令牌桶工作原理
- Uni2D 入门 -- Animation Clip 和 Animation API
- [连载]C#程序设计(11)--- 接口
- 整数快速幂hdu(1852)
- Uni2D 入门 -- Skeletal Animation + Asset Table
- ACTF2014exploit100 餐前甜点
- [字符串]CH Round#30 T1 语言识别
- C++ primer读书笔记7-复制控制