最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

本文主要研究一下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方法,其中generateSalt方法根據(jù)指定的長度參數(shù)來生成salt,而includePlainSaltInEncryptionResults則返回是否需要將salt包含在加密結果中,通常對于隨機生成的需要返回true,對于固定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,它沒有新定義方法,僅僅是作為接口標識,ByteArrayFixedSaltGenerator、StringFixedSaltGenerator都實現(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的構造器要求輸入salt的byte數(shù)組,其的generateSalt要求請求的lengthBytes小于等于salt的長度,否則拋出EncryptionInitializationException異常,對于salt的長度大于請求的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則返回一個空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進行隨機生成salt,其includePlainSaltInEncryptionResults返回true

小結

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

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

相關文章

  • SpringMvc使用GoogleKaptcha生成驗證碼

    SpringMvc使用GoogleKaptcha生成驗證碼

    這篇文章主要為大家詳細介紹了SpringMvc項目中使用GoogleKaptcha 生成驗證碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 微信小程序微信登錄的實現(xiàn)方法詳解(JAVA后臺)

    微信小程序微信登錄的實現(xiàn)方法詳解(JAVA后臺)

    通常我們在登錄微信小程序的時候都是通過授權登錄,下面這篇文章主要給大家介紹了關于微信小程序微信登錄的實現(xiàn)方法,文中通過實例代碼介紹的介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • java json 省市級聯(lián)實例代碼

    java json 省市級聯(lián)實例代碼

    這篇文章介紹了java json 省市級聯(lián)實例代碼,有需要的朋友可以參考一下
    2013-09-09
  • jpanel設置背景圖片的二個小例子

    jpanel設置背景圖片的二個小例子

    這篇文章主要介紹了jpanel設置背景圖片的二個小例子,實現(xiàn)了動態(tài)加載圖片做背景的方法,需要的朋友可以參考下
    2014-03-03
  • RestTemplate調(diào)用POST和GET請求示例詳解

    RestTemplate調(diào)用POST和GET請求示例詳解

    這篇文章主要為大家介紹了RestTemplate調(diào)用POST和GET請求示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Java中的concurrenthashmap集合詳細剖析

    Java中的concurrenthashmap集合詳細剖析

    這篇文章主要介紹了Java中的concurrenthashmap集合詳細剖析,有參構造后第一次put時會進行初始化,由源碼可知,會先判斷所傳入的容量是否>=最大容量的一半,如果滿足條件,就會將容量修改為最大值,反之則會將容量改為所傳入數(shù)*1.5+1,需要的朋友可以參考下
    2023-11-11
  • @FeignClient?path屬性路徑前綴帶路徑變量時報錯的解決

    @FeignClient?path屬性路徑前綴帶路徑變量時報錯的解決

    這篇文章主要介紹了@FeignClient?path屬性路徑前綴帶路徑變量時報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java Swing 只關閉當前窗體的實現(xiàn)

    Java Swing 只關閉當前窗體的實現(xiàn)

    這篇文章主要介紹了Java Swing 只關閉當前窗體的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • java實現(xiàn)五子棋小游戲

    java實現(xiàn)五子棋小游戲

    這篇文章主要介紹了java實現(xiàn)五子棋小游戲的相關資料,十分簡單實用,推薦給大家,需要的朋友可以參考下
    2015-03-03
  • Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件

    Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件

    這篇文章主要介紹了Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件,下面文章圍繞FileUtils的相關資料展開怎么讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件的內(nèi)容,具有一定的參考價值,徐婭奧德小伙伴可以參考一下
    2021-12-12

最新評論

舞阳县| 田东县| 滕州市| 浦城县| 大姚县| 绥棱县| 上栗县| 绍兴市| 望城县| 绵阳市| 元阳县| 迭部县| 潜江市| 资兴市| 通海县| 措美县| 航空| 芜湖市| 泗水县| 汶上县| 三江| 昌江| 新邵县| 鹤壁市| 南安市| 台中市| 桑日县| 涡阳县| 陆良县| 兴隆县| 邓州市| 富宁县| 广宁县| 泰兴市| 宜章县| 福贡县| 绥滨县| 岚皋县| 鹿泉市| 九寨沟县| 宁德市|