shiro密码加密

	/**
     * base64编码和解码
     */
    @Test
    public void testBase64() {
        String str = "hello";
        //编码
        //String encodeToString = Base64.encodeToString(str.getBytes());
        String encodeToString = Base64.encodeToString(CodecSupport.toBytes(str));
        System.out.println(encodeToString);

        //解码
        String decodeToString = Base64.decodeToString(encodeToString);
        System.out.println(decodeToString);
    }

    /**
     * 十六进制的编码和解码
     */
    @Test
    public void testHex() {
        String str = "hello";
        //编码
        String encodeToString = Hex.encodeToString(str.getBytes());
        System.out.println(encodeToString);
        //解码
        String decodeToString = new String(Hex.decode(encodeToString));
        System.out.println(decodeToString);
    }

    /**
     * md5加密
     */
    @Test
    public void testMd5(){
        String str="123";
        String s1 = new Md5Hash(str).toString();
        String s2 = new Md5Hash(str).toBase64();
        String s3 = new Md5Hash(str).toHex();
        String s4 = new Md5Hash(str, "anycode", 3).toString(); //加密时指定salt盐值和散列次数
        
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }

    /**
     * sha加密
     * SHA1、SHA256、SHA384、SHA512
     */
    @Test
    public void testSha(){
        String str="123";
        String s1 = new Sha1Hash(str).toString();
        String s2 = new Sha256Hash(str).toBase64();
        String s3 = new Sha384Hash(str, "anycode", 2).toHex();
        String s4 = new Sha512Hash(str, "anycode", 2).toHex();

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }

    /**
     * 通用散列算法的工具类SimpleHash
     */
    @Test
    public void testSimpleHash(){
        String str="123";

        String s1 = new SimpleHash("md5", str, "anycode", 3).toString();
        String s2 = new SimpleHash("sha-256", str, "anycode", 3).toString();

        System.out.println(s1);
        System.out.println(s2);
    }

    /**
     * PasswordService
     */
    @Test
    public void testPwd(){
        String password="123";

        //PasswordService passwordService=new DefaultPasswordService();
        //String encryptPassword = passwordService.encryptPassword(password);
        //System.out.println(encryptPassword);
        //boolean flag = passwordService.passwordsMatch(password, encryptPassword);
        //System.out.println(flag);

        PasswordServiceImpl passwordService=new PasswordServiceImpl();
        passwordService.setAlgorithmName("md5");
        passwordService.setSalt("anycode");
        passwordService.setHashInteration(3);
        String encryptPassword = passwordService.encryptPassword(password);

        boolean flag = passwordService.passwordsMatch(password, encryptPassword);
        System.out.println(flag);

    }


    /**
     * 模拟用户注册
     */
    @Test
    public void regist(){
        String username="admin";
        String password="111";

        System.out.println(new SimpleHash("md5",password,username,3).toString());
        System.out.println(new SimpleHash("md5",password,username,3).toString());
        System.out.println(new SimpleHash("md5","222","tom",3).toString());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值