shiro之编码加密

一、Shiro 提供了base64和16进制字符串编码/解码的API支持:

	    String str = "h";
		//base64 编码
		String base64Encoded = Base64.encodeToString(str.getBytes());
		//base64 编码解码
		String str2 = Base64.decodeToString(base64Encoded);
		System.out.println(str.equals(str2));
		
		String str3 = "y";
		//16进制字符串编码
		String hexEncoded = Hex.encodeToString(str3.getBytes());
		//16进制字符串解码
		String str4 = new String(Hex.decode(hexEncoded));
		System.out.println(str3.equals(str4));
		//byte数组与String转换
		CodecSupport.toBytes(str, "utf-8");
		CodecSupport.toString(str.getBytes(), "utf-8");

二、散列算法

散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如MD5、SHA等。

        String str = "h";
		String salt = "123"; //盐
		//单纯md5加密,易破解
		System.out.println(new Md5Hash(str).toString());
		//加干扰数据,即盐
		System.out.println(new Md5Hash(str,salt).toString());
		//指定散列次数,如2次::md5(md5(str))
		System.out.println(new Md5Hash(str,salt,2).toString());
		//SHA256 算法 还有如SHA1、SHA512算法
		System.out.println(new Sha256Hash(str,salt).toString());
		//shiro通用散列支持,内部使用了Java 的MessageDigest实现
		System.out.println(new SimpleHash("md5",str,salt).toString());

Shiro 提供了HashService,默认提供了DefaultHashService实现

        //创建一个DefaultHashService,默认使用SHA-512 算法;
		DefaultHashService hashService = new DefaultHashService();
		//修改算法
		hashService.setHashAlgorithmName("md5");
		//设置一个私盐,其在散列时自动与用户传入的公盐混合产生一个新盐
		hashService.setPrivateSalt(new SimpleByteSource("123"));
		//在用户没有传入公盐的情况下是否生成公盐,默认false
		hashService.setGeneratePublicSalt(true);
		//用于生成公盐
		hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());
		//修改默认加密迭代次数
		hashService.setHashIterations(2);
		//构建一个HashRequest,传入算法、数据、公盐、迭代次数。
		HashRequest request = new HashRequest.Builder().setAlgorithmName("md5")
				.setSource(ByteSource.Util.bytes("h"))
				.setSalt(ByteSource.Util.bytes("123"))
				.setIterations(2).build();
		System.out.println(hashService.computeHash(request).toString());
		
		//SecureRandomNumberGenerator用于生成一个随机数
		SecureRandomNumberGenerator srng = new SecureRandomNumberGenerator();
		srng.setSeed("123".getBytes());
		System.out.println(srng.nextBytes().toHex());

三、加密、解密

Shiro 还提供对称式加密/解密算法的支持,如AES、Blowfish 等

        //AES算法
		AesCipherService acs = new AesCipherService();
		acs.setKeySize(128);//key长度
		Key key = acs.generateNewKey();//生成key
		String str = "h";
		//加密
		String encrptText = acs.encrypt(str.getBytes(), key.getEncoded()).toHex();
		System.out.println(encrptText);
		//解密
		String str2 = new String(acs.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes());
		System.out.println(str2);

四、PasswordService/CredentialsMatcher

Shiro 提供了PasswordService及CredentialsMatcher用于提供加密密码及验证密码服务。

public interface PasswordService {
//输入明文密码得到密文密码
String encryptPassword(Object plaintextPassword) throws IllegalArgumentException;
}
public interface CredentialsMatcher {
//匹配用户输入的token 的凭证(未加密)与系统提供的凭证(已加密)
boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info);
}

Shiro 默认提供了PasswordService 实现DefaultPasswordService;CredentialsMatcher 实现PasswordMatcher及HashedCredentialsMatcher

简单使用:

service层加密密码保存到数据库

//加密密码
user.setPassword(passwordService.encryptPassword(user.getPassword()));

登录时realm验证

SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
				account,user.getPassword(),this.getName());
		System.out.println("匹配=>"+credentialsMatcher.doCredentialsMatch(token,authenticationInfo));
		if( !credentialsMatcher.doCredentialsMatch(token,authenticationInfo)){
			throw new IncorrectCredentialsException();
		}

 

转载于:https://my.oschina.net/xinyuanKong/blog/707853

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值