org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder

密码,加密,解密

spring-security-crypto-5.7.3.jar

/** Copyright 2002-2011 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.security.crypto.bcrypt;import java.security.SecureRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import org.springframework.security.crypto.password.PasswordEncoder;/*** Implementation of PasswordEncoder that uses the BCrypt strong hashing function. Clients* can optionally supply a "version" ($2a, $2b, $2y) and a "strength" (a.k.a. log rounds* in BCrypt) and a SecureRandom instance. The larger the strength parameter the more work* will have to be done (exponentially) to hash the passwords. The default value is 10.** @author Dave Syer*/
public class BCryptPasswordEncoder implements PasswordEncoder {private Pattern BCRYPT_PATTERN = Pattern.compile("\\A\\$2(a|y|b)?\\$(\\d\\d)\\$[./0-9A-Za-z]{53}");private final Log logger = LogFactory.getLog(getClass());private final int strength;private final BCryptVersion version;private final SecureRandom random;public BCryptPasswordEncoder() {this(-1);}/*** @param strength the log rounds to use, between 4 and 31*/public BCryptPasswordEncoder(int strength) {this(strength, null);}/*** @param version the version of bcrypt, can be 2a,2b,2y*/public BCryptPasswordEncoder(BCryptVersion version) {this(version, null);}/*** @param version the version of bcrypt, can be 2a,2b,2y* @param random the secure random instance to use*/public BCryptPasswordEncoder(BCryptVersion version, SecureRandom random) {this(version, -1, random);}/*** @param strength the log rounds to use, between 4 and 31* @param random the secure random instance to use*/public BCryptPasswordEncoder(int strength, SecureRandom random) {this(BCryptVersion.$2A, strength, random);}/*** @param version the version of bcrypt, can be 2a,2b,2y* @param strength the log rounds to use, between 4 and 31*/public BCryptPasswordEncoder(BCryptVersion version, int strength) {this(version, strength, null);}/*** @param version the version of bcrypt, can be 2a,2b,2y* @param strength the log rounds to use, between 4 and 31* @param random the secure random instance to use*/public BCryptPasswordEncoder(BCryptVersion version, int strength, SecureRandom random) {if (strength != -1 && (strength < BCrypt.MIN_LOG_ROUNDS || strength > BCrypt.MAX_LOG_ROUNDS)) {throw new IllegalArgumentException("Bad strength");}this.version = version;this.strength = (strength == -1) ? 10 : strength;this.random = random;}@Overridepublic String encode(CharSequence rawPassword) {if (rawPassword == null) {throw new IllegalArgumentException("rawPassword cannot be null");}String salt = getSalt();return BCrypt.hashpw(rawPassword.toString(), salt);}private String getSalt() {if (this.random != null) {return BCrypt.gensalt(this.version.getVersion(), this.strength, this.random);}return BCrypt.gensalt(this.version.getVersion(), this.strength);}@Overridepublic boolean matches(CharSequence rawPassword, String encodedPassword) {if (rawPassword == null) {throw new IllegalArgumentException("rawPassword cannot be null");}if (encodedPassword == null || encodedPassword.length() == 0) {this.logger.warn("Empty encoded password");return false;}if (!this.BCRYPT_PATTERN.matcher(encodedPassword).matches()) {this.logger.warn("Encoded password does not look like BCrypt");return false;}return BCrypt.checkpw(rawPassword.toString(), encodedPassword);}@Overridepublic boolean upgradeEncoding(String encodedPassword) {if (encodedPassword == null || encodedPassword.length() == 0) {this.logger.warn("Empty encoded password");return false;}Matcher matcher = this.BCRYPT_PATTERN.matcher(encodedPassword);if (!matcher.matches()) {throw new IllegalArgumentException("Encoded password does not look like BCrypt: " + encodedPassword);}int strength = Integer.parseInt(matcher.group(2));return strength < this.strength;}/*** Stores the default bcrypt version for use in configuration.** @author Lin Feng*/public enum BCryptVersion {$2A("$2a"),$2Y("$2y"),$2B("$2b");private final String version;BCryptVersion(String version) {this.version = version;}public String getVersion() {return this.version;}}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/168010.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Kafka(一)

一&#xff1a;简介 解决高吞吐量项目的需求 是一款为大数据而生的消息中间件&#xff0c;具有百亿级tps的吞吐量&#xff0c;在数据采集、传输、存储的过程中发挥着作用 二&#xff1a;为什么要使用消息队列 一个普通访问量的接口和一个大并发的接口&#xff0c;它们背后的…

C/C++---------------LeetCode第1512. 好数对的数目

好数对的数目 题目及要求暴力算法哈希算法在main内使用 题目及要求 给你一个整数数组 nums 。 如果一组数字 (i,j) 满足 nums[i] nums[j] 且 i < j &#xff0c;就可以认为这是一组 好数对 。 返回好数对的数目。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3,1,…

376.摆动序列

原题链接&#xff1a;376.摆动序列 全代码&#xff1a; class Solution { public:int wiggleMaxLength(vector<int>& nums) {if (nums.size() < 1) return nums.size();int curDiff 0; // 当前一对差值int preDiff 0; // 前一对差值int result 1; // 记录峰…

Android骨架图

用法&#xff1a;在图片上实现动画效果 <FrameLayoutandroid:id"id/image_container"android:layout_width"match_parent"android:layout_height"wrap_content"><ImageViewandroid:id"id/ivBlank"android:layout_width"…

PostgreSQL Patroni 3.0 新功能规划 2023年 纽约PG 大会 (音译)

开头还是介绍一下群&#xff0c;如果感兴趣PolarDB ,MongoDB ,MySQL ,PostgreSQL ,Redis, Oceanbase, Sql Server等有问题&#xff0c;有需求都可以加群群内有各大数据库行业大咖&#xff0c;CTO&#xff0c;可以解决你的问题。加群请联系 liuaustin3 &#xff0c;&#xff08;…

React Hooks函数之useRef

useRef 是 React 中常用的 Hook 之一&#xff0c;它返回一个可变的 ref 对象&#xff0c;其 .current 属性被初始化为传入的参数&#xff08;initialValue&#xff09;。返回的 ref 对象在组件的整个生命周期内保持不变。 以下是一些使用 useRef 的场景和示例&#xff1a; 1、…

Mathorcup数学建模竞赛第一届-【妈妈杯】B题:图像识别

目录 知识储备 传统图像处理方法进行瑕疵检测 传统算法方向的选择 瑕疵检测关注的两个问题 瑕疵的标注

【技术分享】RK3399 Ubuntu通过Python实现录音和播放功能

​本文基于IDO-SBC3968 Ubuntu 系统通过Python脚本实现录音和播放功能。 IDO-SBC3968采用RK3399国产六核64位CPU高性能处理器&#xff0c;支持4K HDMI2.0显示&#xff0c;接口丰富&#xff0c;拥有千兆以太网&#xff0c;全协议TypeC接口&#xff0c;USB3.0 &#xff0c;eDP 和…

Redis高并发缓存架构

前言&#xff1a; 针对缓存我们并不陌生&#xff0c;而今天所讲的是使用redis作为缓存工具进行缓存数据。redis缓存是将数据保存在内存中的&#xff0c;而内存的珍贵性是不可否认的。所以在缓存之前&#xff0c;我们需要明确缓存的对象&#xff0c;是否有必要缓存&#xff0c;怎…

ElasticSearch之配置

ElasticSearch主要的配置文件&#xff0c;如下&#xff1a; elasticsearch.yml&#xff0c;ElasticSearch的相关参数。jvm.options&#xff0c;JVM的相关参数。log4j2.properties&#xff0c;日志的相关参数。 默认情况下&#xff0c;ElasticSearch从$ES_HOME/config目录下读…

1 动态规划解不同的子序列

来源&#xff1a; LeetCode第115题 难度&#xff1a; 困难 问题描述 给定一个字符串S和一个字符串t&#xff0c;计算在S的子序列中t出现的个数。 注解&#xff1a; 字符串的一个子序列是指&#xff0c;通过删除一些(也可以不删除)字符且不干扰剩余字符的相对位置所组成的…

2022年03月 Scratch(三级)真题解析#中国电子学会#全国青少年软件编程等级考试

Scratch等级考试(1~4级)全部真题・点这里 一、单选题(共25题,每题2分,共50分) 第1题 以下四个选项中,运行哪个积木块,可能得到523这个数值? A: B: C: D: 答案:B 四个选项都遵循统一的公式:随机数ⅹ10+3=523,因此可以得出随

ZXing创建二维码位图

Android生成二维码通常使用ZXing库&#xff0c;其中提供了QRCodeWriter类。QRCodeWriter可将字符串编译为位矩阵BitMatrix&#xff0c;然后我们可以将位矩阵转为Int数组&#xff0c;通过bitmap.setPixels()方法将数组绘制于位图上。 1. 添加依赖 //Gradle Scripts -> buil…

和数集团出席中科院上海高研院​第三十三期“高研交叉论坛”信息能源融合专场

2023年11月21日&#xff0c;中国科学院上海高等研究院第三十三期“高研交叉论坛”信息能源融合专场在上海高研院成功举办。本次论坛由中国科学院上海高等研究院智能信息通信技术研究与发展中心、中国科学院低碳转化科学与工程重点实验室、中科院和数智能区块链与能源系统应用联…

【文末送书】机器学习高级实践

2023年初是人工智能爆发的里程碑式的重要阶段&#xff0c;以OpenAI研发的GPT为代表的大模型大行其道&#xff0c;NLP领域的ChatGPT模型火爆一时&#xff0c;引发了全民热议。而最新更新的GPT-4更是实现了大型多模态模型的飞跃式提升&#xff0c;它能够同时接受图像和文本的输入…

仿 美图 / 饿了么,店铺详情页功能

前言 UI有所不同&#xff0c;但功能差不多&#xff0c;商品添加购物车功能 正在写&#xff0c;写完会提交仓库。 效果图一&#xff1a;左右RecyclerView 联动 效果图二&#xff1a;通过点击 向上偏移至最大值 效果图三&#xff1a;通过点击 或 拖动 展开收缩公告 效果图四&…

SpringBoot3核心原理

SpringBoot3核心原理 事件和监听器 生命周期监听 场景&#xff1a;监听应用的生命周期 可以通过下面步骤自定义SpringApplicationRunListener来监听事件。 ①、编写SpringApplicationRunListener实现类 ②、在META-INF/spring.factories中配置org.springframework.boot.Sprin…

上市公司-股权性质数据(国企、央企)2003-2022年

上市公司-股权性质数据&#xff08;国企、央企&#xff09;是一个针对上市公司的数据集&#xff0c;主要涵盖了A股公司股权性质的详细信息&#xff0c;区分了公司是否为民营企业、国企或央企。这份数据集提供了每家上市公司的股权结构背景&#xff0c;对投资者、市场分析师和经…

Shell循环:for(二)

一、通过用户列表文件创建用户 需求&#xff1a;通过用户列表文件创建用户 [rootlocalhost ~]# cat user.txt qian yoa huang演示&#xff1a; [rootlocalhost ~]# vim foruser.sh #编写脚本 #!/bin/bash for i in cat user.txt do useradd $i if [ $? -eq 0 ] thenech…

2023年亚太杯数学建模C题新能源汽车成品文章(思路模型代码成品)

一、翻译 新能源汽车是指采用先进的技术原理、新技术和新结构&#xff0c;以非常规车用燃料&#xff08;非常规车用燃料是指汽油和柴油以外的燃料(非常规车用燃料是指汽油和柴油以外的燃料&#xff09;&#xff0c;并集成了汽车动力控制和驱动等先进技术的汽车。新能源汽车包括…