前言
你是否存在这样的苦恼,数据需要安全存储,但是每个系统大家自己写,很浪费时间。
encryption-local 一个离线版本的金融敏感信息加解密工具,用于数据库敏感信息存储。
本文介绍一下用户姓名的加解密+掩码源码解析。
快速开始
maven 引入
[xml]
1
2
3
4
5<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>encryption-local-core</artifactId>
<version>1.2.0</version>
</dependency>
例子
[java]
1
2
3
4
5
6
7
8
9
10
11
12
13final String passwordText = "123456";
final String salt = "99886622";
CommonEncryptResponse response = EncryptionLocalUtil.passwordEncrypt(passwordText, salt);
String cipher = response.getCipher();
String mask = response.getMask();
String hash = response.getHash();
Assert.assertEquals("8B208237BEB2E6A4390E7128E5E000D7", cipher);
Assert.assertEquals("******", mask);
Assert.assertEquals("FEB408A10822A55A939E8E38A6612515", hash);
// 解密
String plain = EncryptionLocalUtil.passwordDecrypt(cipher, salt);
Assert.assertEquals(passwordText, plain);
源码部分
这里主要重点分析一下掩码的源码部分,我们可以直接在 encryption-local 查看源码。
[java]
1
2
3
4
5
6
7
8public class EncryptMaskPassword extends AbstractEncryptMask {
@Override
protected String doMask(EncryptMaskContext context) {
return InnerMaskUtil.password(context.getPlainText());
}
}
密码掩码的核心实现其实很简单,我们直接相同个数的 *
,或者直接返回空都行。
[java]
1
2
3
4
5
6
7
8
9
10
11
12/**
* 密码信息脱敏
* @param password 地址
* @return 脱敏结果
* @since 1.1.0
*/
public static String password(final String password) {
if(StringUtil.isEmpty(password)) {
return password;
}
return StringUtil.repeat("*", password.length());
}
当然,我们完全可以根据这个进行自己的实现拓展。
拓展阅读
项目推荐
下面是一些日志、加解密、脱敏安全相关的库推荐:
项目 | 介绍 |
---|---|
sensitive-word | 高性能敏感词核心库 |
sensitive-word-admin | 敏感词控台,前后端分离 |
sensitive | 高性能日志脱敏组件 |
auto-log | 统一日志切面组件,支持全链路traceId |
encryption-local | 离线加密机组件 |
encryption | 加密机标准API+本地客户端 |
encryption-server | 加密机服务 |
拓展阅读
【老马】离线版金融敏感信息加解密组件开源项目encryption-local
小结
离线版本的加解密好处是非常的方便,如果需要的话建议直接使用,可以节约很多时间。
希望这个项目可以帮助到你。喜欢的话,可以给原作者一个 star 鼓励一下!
我是老马,期待与你的下次重逢~