前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >聊聊jasypt的SaltGenerator

聊聊jasypt的SaltGenerator

作者头像
code4it
发布于 2023-09-12 11:53:11
发布于 2023-09-12 11:53:11
18400
代码可运行
举报
文章被收录于专栏:码匠的流水账码匠的流水账
运行总次数:0
代码可运行

本文主要研究一下jasypt的SaltGenerator

SaltGenerator

org/jasypt/salt/SaltGenerator.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * <p>
 * Common interface for all salt generators which can be applied in digest
 * or encryption operations.
 * </p>
 * <p>
 * <b>Every implementation of this interface must be thread-safe</b>.
 * </p>
 * 
 * @since 1.2
 * 
 * @author Daniel Fern&aacute;ndez
 * 
 */
public interface SaltGenerator {

    /**
     * <p>
     * This method will be called for requesting the generation of a new
     * salt of the specified length.
     * </p>
     * 
     * @param lengthBytes the requested length for the salt. 
     * @return the generated salt.
     */
    public byte[] generateSalt(int lengthBytes);
    
    
    /**
     * <p>
     * Determines if the digests and encrypted messages created with a 
     * specific salt generator will include (prepended) the unencrypted 
     * salt itself, so that it can be used for matching and decryption 
     * operations.
     * </p>
     * <p>
     * Generally, including the salt unencrypted in encryption results will 
     * be mandatory for randomly generated salts, or for those generated in a 
     * non-predictable manner.
     * Otherwise, digest matching and decryption operations will always fail.
     * For fixed salts, inclusion will be optional (and in fact undesirable 
     * if we want to hide the salt value).
     * </p>    
     * 
     * @return whether the plain (unencrypted) salt has to be included in 
     *         encryption results or not.
     */
    public boolean includePlainSaltInEncryptionResults();

    
}

SaltGenerator接口定义了generateSalt及includePlainSaltInEncryptionResults方法,其中generateSalt方法根据指定的长度参数来生成salt,而includePlainSaltInEncryptionResults则返回是否需要将salt包含在加密结果中,通常对于随机生成的需要返回true,对于固定salt的则不需要,它有几类,分别是FixedSaltGenerator、ZeroSaltGenerator、RandomSaltGenerator

FixedSaltGenerator

org/jasypt/salt/FixedSaltGenerator.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * <p>
 * Marker interface for all implementations of {@link SaltGenerator} that
 * will always return the same salt (for the same amount of bytes asked).
 * </p>
 * <p>
 * Use of this interface in salt generators enables encryptors to perform
 * some performance optimizations whenever they are used.
 * </p>
 * 
 * @since 1.9.2
 * 
 * @author Daniel Fern&aacute;ndez
 * 
 */
public interface FixedSaltGenerator extends SaltGenerator {

    // Marker interface - no methods added
    
}

FixedSaltGenerator继承了SaltGenerator,它没有新定义方法,仅仅是作为接口标识,ByteArrayFixedSaltGenerator、StringFixedSaltGenerator都实现了FixedSaltGenerator接口

ByteArrayFixedSaltGenerator

org/jasypt/salt/ByteArrayFixedSaltGenerator.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class ByteArrayFixedSaltGenerator implements FixedSaltGenerator {

    private final byte[] salt;
    
    /**
     * Creates a new instance of <tt>FixedByteArraySaltGenerator</tt>
     *
     * @param salt the specified salt.
     */
    public ByteArrayFixedSaltGenerator(final byte[] salt) {
        super();
        CommonUtils.validateNotNull(salt, "Salt cannot be set null");
        this.salt = (byte[]) salt.clone();
    }

    
    /**
     * Return salt with the specified byte length.
     * 
     * @param lengthBytes length in bytes.
     * @return the generated salt. 
     */
    public byte[] generateSalt(final int lengthBytes) {
        if (this.salt.length < lengthBytes) {
            throw new EncryptionInitializationException(
                    "Requested salt larger than set");
        }
        final byte[] generatedSalt = new byte[lengthBytes];
        System.arraycopy(this.salt, 0, generatedSalt, 0, lengthBytes);
        return generatedSalt;
    }


    /**
     * As this salt generator provides a fixed salt, its inclusion 
     * unencrypted in encryption results
     * is not necessary, and in fact not desirable (so that it remains hidden).
     * 
     * @return false
     */
    public boolean includePlainSaltInEncryptionResults() {
        return false;
    }

    
}

ByteArrayFixedSaltGenerator的构造器要求输入salt的byte数组,其的generateSalt要求请求的lengthBytes小于等于salt的长度,否则抛出EncryptionInitializationException异常,对于salt的长度大于请求的lengthBytes的,则取前面的lengthBytes;其includePlainSaltInEncryptionResults返回false

StringFixedSaltGenerator

org/jasypt/salt/StringFixedSaltGenerator.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class StringFixedSaltGenerator implements FixedSaltGenerator {

    private static final String DEFAULT_CHARSET = "UTF-8";
    
    private final String salt;
    private final String charset;
    private final byte[] saltBytes;

    
    
    /**
     * Creates a new instance of <tt>FixedStringSaltGenerator</tt> using
     * the default charset.
     *
     * @param salt the specified salt.
     */
    public StringFixedSaltGenerator(final String salt) {
        this(salt, null);
    }

    
    /**
     * Creates a new instance of <tt>FixedStringSaltGenerator</tt>
     *
     * @param salt the specified salt.
     * @param charset the specified charset
     */
    public StringFixedSaltGenerator(final String salt, final String charset) {
        super();
        CommonUtils.validateNotNull(salt, "Salt cannot be set null");
        this.salt = salt;
        this.charset = (charset != null? charset : DEFAULT_CHARSET);
        try {
            this.saltBytes = this.salt.getBytes(this.charset);
        } catch (UnsupportedEncodingException e) {
            throw new EncryptionInitializationException(
                "Invalid charset specified: " + this.charset);
        }
    }

    
    /**
     * Return salt with the specified byte length.
     * 
     * @param lengthBytes length in bytes.
     * @return the generated salt. 
     */
    public byte[] generateSalt(final int lengthBytes) {
        if (this.saltBytes.length < lengthBytes) {
            throw new EncryptionInitializationException(
                    "Requested salt larger than set");
        }
        final byte[] generatedSalt = new byte[lengthBytes];
        System.arraycopy(this.saltBytes, 0, generatedSalt, 0, lengthBytes);
        return generatedSalt;
    }


    /**
     * As this salt generator provides a fixed salt, its inclusion 
     * unencrypted in encryption results
     * is not necessary, and in fact not desirable (so that it remains hidden).
     * 
     * @return false
     */
    public boolean includePlainSaltInEncryptionResults() {
        return false;
    }

    
}

StringFixedSaltGenerator跟ByteArrayFixedSaltGenerator类似,只不过入参是String类型,但内部是转为byte[]类型

ZeroSaltGenerator

org/jasypt/salt/ZeroSaltGenerator.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class ZeroSaltGenerator implements SaltGenerator {
    
    /**
     * Creates a new instance of <tt>ZeroSaltGenerator</tt>
     *
     */
    public ZeroSaltGenerator() {
        super();
    }

    
    /**
     * Return salt with the specified byte length. This will return
     * an array of <i>zero</i> bytes, with the specified length.
     * 
     * @param lengthBytes length in bytes.
     * @return the generated salt. 
     */
    public byte[] generateSalt(final int lengthBytes) {
        final byte[] result = new byte[lengthBytes];
        Arrays.fill(result, (byte)0);
        return result;
    }


    /**
     * As this salt generator provides a predictable salt, its inclusion 
     * unencrypted in encryption results
     * is not necessary, and in fact not desirable (so that it remains hidden).
     * 
     * @return false
     */
    public boolean includePlainSaltInEncryptionResults() {
        return false;
    }

    
}

ZeroSaltGenerator则返回一个空byte[]

RandomSaltGenerator

org/jasypt/salt/RandomSaltGenerator.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class RandomSaltGenerator implements SaltGenerator {
    
    /**
     * The default algorithm to be used for secure random number 
     * generation: set to SHA1PRNG.
     */
    public static final String DEFAULT_SECURE_RANDOM_ALGORITHM = "SHA1PRNG";
    
    private final SecureRandom random;
    
    
    /**
     * Creates a new instance of <tt>RandomSaltGenerator</tt> using the 
     * default secure random number generation algorithm.
     */
    public RandomSaltGenerator() {
        this(DEFAULT_SECURE_RANDOM_ALGORITHM);
    }
    
    
    /**
     * Creates a new instance of <tt>RandomSaltGenerator</tt> specifying a 
     * secure random number generation algorithm.
     * 
     * @since 1.5
     * 
     */
    public RandomSaltGenerator(final String secureRandomAlgorithm) {
        super();
        try {
            this.random = SecureRandom.getInstance(secureRandomAlgorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new EncryptionInitializationException(e);
        }
    }
    

    /**
     * Generate a random salt of the specified length in bytes.
     * 
     * @param lengthBytes length in bytes.
     * @return the generated salt. 
     */
    public byte[] generateSalt(final int lengthBytes) {
        final byte[] salt = new byte[lengthBytes];
        synchronized (this.random) {
            this.random.nextBytes(salt);
        }
        return salt;
    }


    /**
     * This salt generator needs the salt to be included unencrypted in 
     * encryption results, because of its being random. This method will always 
     * return true.
     * 
     * @return true
     */
    public boolean includePlainSaltInEncryptionResults() {
        return true;
    }

    
}

RandomSaltGenerator采取的是SHA1PRNG的SecureRandom进行随机生成salt,其includePlainSaltInEncryptionResults返回true

小结

SaltGenerator接口定义了generateSalt及includePlainSaltInEncryptionResults方法,其中generateSalt方法根据指定的长度参数来生成salt,而includePlainSaltInEncryptionResults则返回是否需要将salt包含在加密结果中,通常对于随机生成的需要返回true,对于固定salt的则不需要,它有几类,分别是FixedSaltGenerator、ZeroSaltGenerator、RandomSaltGenerator。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-09-02 11:00,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
VMWARE 虚拟机导入腾讯云-离线迁移篇
对待迁移的虚拟机进行准备,包括检查并安装Virtio 驱动、安装 cloud-init。
本地专用集群CDC
2023/04/13
3.2K0
腾讯云对象存储
  对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
别团等shy哥发育
2023/02/25
58.8K2
腾讯云对象存储
鱼与熊掌可以兼得,腾讯云推出COS on CDC对象存储服务
在云计算时代,各个企业的信息基础设施都在进行云化转型,一时间,似乎万物皆可上云。但是在上云过程中,有不少客户遇到过这样的问题:有部分数据出于合规需求,需要在本地存储与处理,怎么办?一些数据需要本地高速访问,跟公有云之间的延迟太高了,怎么办?使用私有化方式部署,需要专门的团队来采购、管理、运维,无法享受上云的便利性,怎么办?
云存储
2021/08/19
1.7K0
鱼与熊掌可以兼得,腾讯云推出COS on CDC对象存储服务
使用腾讯云CVM挂载COS作为Emby媒体服务器
多款腾讯云产品免费体验:https://cloud.tencent.com/act/free?from=20863 最近收集了一些4k杜比视界的资源,于是想搭建自己的媒体服务器,考虑到NAS维护麻烦、
4O4
2023/07/05
1.3K0
使用腾讯云CVM挂载COS作为Emby媒体服务器
腾讯云对象存储 COS 高可用解决方案,都在这里了!
上一篇内容我们说到了 COS 是什么“要对象存储做什么,我有女朋友就够了!”的文章。本文将着重介绍如何尽可能的提高腾讯云对象存储 COS 的可用性。
云存储
2019/12/06
2.8K0
腾讯云对象存储 COS 高可用解决方案,都在这里了!
腾讯云主机安装COSFS工具并使用COS对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。
yuanfan2012
2021/01/28
3.8K0
如何使用 S3CMD 访问 COS 服务
S3cmd 是免费的命令行工具和客户端,用于在 Amazon S3 和其他兼容 S3 协议的对象存储中上传、下载和管理数据。本文主要介绍如何使用 S3cmd 访问 COS 上的文件。
吴硕卫
2020/04/13
2.6K0
【技术创作101训练营】腾讯云主机安装COSFS工具并使用COS对象存储
大家好 ,我叫袁繁,昵称yuanfan2012,人送外号“袁老”,没办法年龄确实是我的硬伤,所以同事常叫我“袁老”,运维攻城狮一名
yuanfan2012
2021/01/20
3K0
【技术创作101训练营】腾讯云主机安装COSFS工具并使用COS对象存储
对象存储,了解一下
对象存储,通常指 S3 (Simple Storage Service) 服务,由AWS提供公有云服务,而 Ceph 也可以提供兼容 S3 协议的对象存储服务,使用起来跟 AWS 的 S3 体验几乎一样。 环境介绍 访问域名: tstack-s3.oa.com 后端物理环境: [ 64G/8Core/11TB*4/10GE*2 ] * 5台 Ceph 版本: Jewel 10.2.7 RGW 网关: 1个/台,共5个,HAProxy+KeepAlived 实现负载均衡。 测试秘钥: access_k
腾讯云TStack
2018/10/12
4K0
对象存储,了解一下
腾讯云存储最佳实践系列一:使用对象存储COS托管静态网站
腾讯云开发者社区
2017/12/27
7.9K1
腾讯云存储最佳实践系列一:使用对象存储COS托管静态网站
使用COSCLI六步将对象存储文件迁移至轻量对象存储
由于我的服务器大多都是轻量应用服务器,近期轻量云也推出了个轻量对象存储,套餐包的形式相比cos资源包还是挺划算,最主要的是没有读写请求计费,一个套餐包涵盖所有内容。
小宇-xiaoyu
2024/03/29
4641
Ceph对象存储安装部署及验证
今天来玩下Ceph的对象存储,在开始之前呢,先扯会闲篇,我觉得生活中处处是非结构化数据,最简单的举例,下面两个行业,一个是直播,一个是摄影。
DevinGeng
2019/04/09
2.3K0
Ceph对象存储安装部署及验证
COSCLI 使用实践 - 同步网站附件
COSCLI 是腾讯云对象存储(Cloud Object Storage,COS)提供的客户端命令行工具。通过 COSCLI 工具,您可以通过简单的命令行指令对您 COS 中的对象(Object)实现批量上传、下载、删除等操作。
jwj
2022/05/12
1.5K0
ceph-对象存储
作为文件系统的磁盘,操作系统不能直接访问对象存储。相反,它只能通过应用程序级别的API访问。ceph是一种分布式对象存储系统,通过ceph对象网关提供对象存储接口,也称为RADOS网关(RGW)接口,它构建在ceph RADOS层之上。RGW使用librgw(RADOS Gateway library)和librados,允许应用程序与ceph对象存储建立连接。RGW为应用程序提供了一个RESTful S3/swift兼容的接口,用于在ceph集群中以对象的形式存储数据。ceph还支持多租户对象存储,可以通过RESTful API访问。此外,RGW还支持ceph管理API,可以使用本机API调用来管理ceph存储集群。
yuezhimi
2020/09/30
4K0
使用腾讯云go sdk 查询对象存储中最新文件
我现在想确认某一个对象存储桶的活跃程度,简单的来说。我想知道这个桶里面最后上传的一个文件是什么,以及它的上传时间戳。
对你无可奈何
2024/02/28
3000
如何使用 S3CMD 访问 COS 服务
作者简介 吴硕卫:腾讯云技术支持工程师,现负责腾讯云存储产品的技术支持专项工作。 S3cmd 是免费的命令行工具和客户端,用于在 Amazon S3 和其他兼容 S3 协议的对象存储中上传、下载和管理数据。本文主要介绍如何使用 S3cmd 访问 COS 上的文件。 准备工作 您已注册腾讯云账号,并且从访问管理控制台上获取了腾讯云密钥 SecretID 与 SecretKey。 一、使用环境 1、软件依赖 Python 2.6+/3+ 最新版本的 pip 2、安装及配置 环境安装与配置详细操作请参见 P
云存储
2020/07/31
2.3K0
WordPress如何使用腾讯云对象存储COS存储媒体库附件
这篇文章来介绍一下通过使用插件实现将 WordPress 的媒体库附件存储在腾讯云 COS 上。
沈唁
2023/09/25
1.1K2
腾讯云存储最佳实践系列二:对象存储中配置自定义域名支持 HTTPS 访问
腾讯云开发者社区
2017/12/27
4.7K5
腾讯云存储最佳实践系列二:对象存储中配置自定义域名支持 HTTPS 访问
将 WordPress 多媒体内容存储到腾讯云 COS
WordPress 可以通过第三方插件将多媒体内容保存在腾讯云 COS上,将多媒体内容保存在 COS 上有以下好处:
云存储
2020/01/16
2.2K0
将 WordPress 多媒体内容存储到腾讯云 COS
如何使用亚马逊对象存储AWS S3 SDK访问腾讯云存储COS
COS 提供了 AWS S3 兼容的 API,因此当您的数据从 S3 迁移到 COS 之后,只需要进行简单的配置修改,即可让您的客户端应用轻松兼容 COS 服务。本文主要介绍不同开发平台的 S3 SDK 的适配步骤。在完成添加适配步骤后,您就可以使用 S3 SDK 的接口来访问 COS 上的文件了。
云存储
2020/05/26
4.5K0
相关推荐
VMWARE 虚拟机导入腾讯云-离线迁移篇
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验