欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jasypt SaltGenerator接口定義方法源碼解讀

 更新時(shí)間:2023年09月03日 08:49:43   作者:codecraft  
這篇文章主要為大家介紹了jasypt SaltGenerator接口定義方法源碼解讀,,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下jasypt的SaltGenerator

SaltGenerator

org/jasypt/salt/SaltGenerator.java

/**
 * <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方法,其中g(shù)enerateSalt方法根據(jù)指定的長度參數(shù)來生成salt,而includePlainSaltInEncryptionResults則返回是否需要將salt包含在加密結(jié)果中,通常對(duì)于隨機(jī)生成的需要返回true,對(duì)于固定salt的則不需要,它有幾類,分別是FixedSaltGenerator、ZeroSaltGenerator、RandomSaltGenerator

FixedSaltGenerator

org/jasypt/salt/FixedSaltGenerator.java

/**
 * <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,它沒有新定義方法,僅僅是作為接口標(biāo)識(shí),ByteArrayFixedSaltGenerator、StringFixedSaltGenerator都實(shí)現(xiàn)了FixedSaltGenerator接口

ByteArrayFixedSaltGenerator

org/jasypt/salt/ByteArrayFixedSaltGenerator.java

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的構(gòu)造器要求輸入salt的byte數(shù)組,其的generateSalt要求請(qǐng)求的lengthBytes小于等于salt的長度,否則拋出EncryptionInitializationException異常,對(duì)于salt的長度大于請(qǐng)求的lengthBytes的,則取前面的lengthBytes;其includePlainSaltInEncryptionResults返回false

StringFixedSaltGenerator

org/jasypt/salt/StringFixedSaltGenerator.java

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類似,只不過入?yún)⑹荢tring類型,但內(nèi)部是轉(zhuǎn)為byte[]類型

ZeroSaltGenerator

org/jasypt/salt/ZeroSaltGenerator.java

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則返回一個(gè)空byte[]

RandomSaltGenerator

org/jasypt/salt/RandomSaltGenerator.java

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進(jìn)行隨機(jī)生成salt,其includePlainSaltInEncryptionResults返回true

小結(jié)

SaltGenerator接口定義了generateSalt及includePlainSaltInEncryptionResults方法,其中g(shù)enerateSalt方法根據(jù)指定的長度參數(shù)來生成salt,而includePlainSaltInEncryptionResults則返回是否需要將salt包含在加密結(jié)果中,通常對(duì)于隨機(jī)生成的需要返回true,對(duì)于固定salt的則不需要,它有幾類,分別是FixedSaltGenerator、ZeroSaltGenerator、RandomSaltGenerator。

以上就是jasypt SaltGenerator接口定義方法源碼解讀的詳細(xì)內(nèi)容,更多關(guān)于jasypt SaltGenerator接口定義的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過程解析

    Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過程解析

    這篇文章主要介紹了Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • java中基本注解的知識(shí)點(diǎn)總結(jié)

    java中基本注解的知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于java中基本注解的知識(shí)點(diǎn)總結(jié),有需要的朋友們可以跟著學(xué)習(xí)下。
    2021-06-06
  • 深入了解Java中Synchronized關(guān)鍵字的實(shí)現(xiàn)原理

    深入了解Java中Synchronized關(guān)鍵字的實(shí)現(xiàn)原理

    synchronized是JVM的內(nèi)置鎖,基于Monitor機(jī)制實(shí)現(xiàn),每一個(gè)對(duì)象都有一個(gè)與之關(guān)聯(lián)的監(jiān)視器?(Monitor),這個(gè)監(jiān)視器充當(dāng)了一種互斥鎖的角色,本文就詳細(xì)聊一聊Synchronized關(guān)鍵字的實(shí)現(xiàn)原理,需要的朋友可以參考下
    2023-06-06
  • Java中間的接口用法詳解

    Java中間的接口用法詳解

    Java 程序員都知道要面向接口編程,那 Java? 中的接口除了定義接口方法之外還能怎么用你知道嗎,今天小編就來帶大家看一下 Java 中間的接口還可以有哪些用法,需要的朋友可以參考下
    2023-07-07
  • java微信開發(fā)中的地圖定位功能

    java微信開發(fā)中的地圖定位功能

    本文通過實(shí)例代碼給大家介紹了java微信開發(fā)中的地圖定位功能,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • mybatis-plus使用@EnumValue處理枚舉類型的示例代碼

    mybatis-plus使用@EnumValue處理枚舉類型的示例代碼

    這篇文章主要介紹了mybatis-plus使用@EnumValue處理枚舉類型的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java數(shù)據(jù)庫數(shù)據(jù)分批讀取的實(shí)現(xiàn)示例

    java數(shù)據(jù)庫數(shù)據(jù)分批讀取的實(shí)現(xiàn)示例

    在處理大量數(shù)據(jù)時(shí),直接從數(shù)據(jù)庫一次性讀取所有數(shù)據(jù)可能會(huì)導(dǎo)致內(nèi)存溢出或者性能下降,本文就來介紹一下java數(shù)據(jù)庫數(shù)據(jù)分批讀取的實(shí)現(xiàn)示例,感興趣的可以了解一下
    2024-01-01
  • MyBatis屬性名和字段名不一致的問題解決方法

    MyBatis屬性名和字段名不一致的問題解決方法

    這篇文章給大家詳細(xì)介紹了MyBatis屬性名和字段名不一致的問題解決,文中有詳細(xì)的代碼示例和圖文展示供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下
    2023-12-12
  • Spring Boot之內(nèi)嵌tomcat版本升級(jí)操作示例

    Spring Boot之內(nèi)嵌tomcat版本升級(jí)操作示例

    這篇文章主要為大家介紹了Spring Boot之內(nèi)嵌tomcat版本升級(jí)操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • JavaWeb亂碼問題的終極解決方案(推薦)

    JavaWeb亂碼問題的終極解決方案(推薦)

    這篇文章主要給大家介紹了關(guān)于JavaWeb亂碼問題的終極解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JavaWeb具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評(píng)論