zoukankan      html  css  js  c++  java
  • Spring Security 中的加密BCryptPasswordEncoder

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package org.springframework.security.crypto.bcrypt;
    
    import java.security.SecureRandom;
    import java.util.regex.Pattern;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.security.crypto.password.PasswordEncoder;
    
    public class BCryptPasswordEncoder implements PasswordEncoder {
        private Pattern BCRYPT_PATTERN;
        private final Log logger;
        private final int strength;
        private final SecureRandom random;
    
        public BCryptPasswordEncoder() {
            this(-1);
        }
    
        public BCryptPasswordEncoder(int strength) {
            this(strength, (SecureRandom)null);
        }
    
        public BCryptPasswordEncoder(int strength, SecureRandom random) {
            this.BCRYPT_PATTERN = Pattern.compile("\A\$2a?\$\d\d\$[./0-9A-Za-z]{53}");
            this.logger = LogFactory.getLog(this.getClass());
            if (strength == -1 || strength >= 4 && strength <= 31) {
                this.strength = strength;
                this.random = random;
            } else {
                throw new IllegalArgumentException("Bad strength");
            }
        }
    
        public String encode(CharSequence rawPassword) {
            String salt;
            if (this.strength > 0) {
                if (this.random != null) {
                    salt = BCrypt.gensalt(this.strength, this.random);
                } else {
                    salt = BCrypt.gensalt(this.strength);
                }
            } else {
                salt = BCrypt.gensalt();
            }
    
            return BCrypt.hashpw(rawPassword.toString(), salt);
        }
    
        public boolean matches(CharSequence rawPassword, String encodedPassword) {
            if (encodedPassword != null && encodedPassword.length() != 0) {
                if (!this.BCRYPT_PATTERN.matcher(encodedPassword).matches()) {
                    this.logger.warn("Encoded password does not look like BCrypt");
                    return false;
                } else {
                    return BCrypt.checkpw(rawPassword.toString(), encodedPassword);
                }
            } else {
                this.logger.warn("Empty encoded password");
                return false;
            }
        }
    }
    

      

  • 相关阅读:
    (二)常问升级小点
    (一)常问基础小点
    Linux之expr命令详解
    Mac--Visual Studio Code 常用快捷键
    git撤销commit操作回到add状态
    ExampleMatcher ,在查询非int 或boolean 字段时要使用 withIgnorePaths() 忽略 int 和boolean 字段,要不然查询不到值
    navicat 用url 连接mongo
    javase 打印杨辉三角
    关系型数据库遵循ACID规则
    Redis介绍
  • 原文地址:https://www.cnblogs.com/qianjinyan/p/10534912.html
Copyright © 2011-2022 走看看