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á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á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)文章
SpringMvc使用GoogleKaptcha生成驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了SpringMvc項(xiàng)目中使用GoogleKaptcha 生成驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
微信小程序微信登錄的實(shí)現(xiàn)方法詳解(JAVA后臺(tái))
通常我們?cè)诘卿浳⑿判〕绦虻臅r(shí)候都是通過授權(quán)登錄,下面這篇文章主要給大家介紹了關(guān)于微信小程序微信登錄的實(shí)現(xiàn)方法,文中通過實(shí)例代碼介紹的介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
java json 省市級(jí)聯(lián)實(shí)例代碼
這篇文章介紹了java json 省市級(jí)聯(lián)實(shí)例代碼,有需要的朋友可以參考一下2013-09-09
RestTemplate調(diào)用POST和GET請(qǐng)求示例詳解
這篇文章主要為大家介紹了RestTemplate調(diào)用POST和GET請(qǐng)求示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Java中的concurrenthashmap集合詳細(xì)剖析
這篇文章主要介紹了Java中的concurrenthashmap集合詳細(xì)剖析,有參構(gòu)造后第一次put時(shí)會(huì)進(jìn)行初始化,由源碼可知,會(huì)先判斷所傳入的容量是否>=最大容量的一半,如果滿足條件,就會(huì)將容量修改為最大值,反之則會(huì)將容量改為所傳入數(shù)*1.5+1,需要的朋友可以參考下2023-11-11
@FeignClient?path屬性路徑前綴帶路徑變量時(shí)報(bào)錯(cuò)的解決
這篇文章主要介紹了@FeignClient?path屬性路徑前綴帶路徑變量時(shí)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Java Swing 只關(guān)閉當(dāng)前窗體的實(shí)現(xiàn)
這篇文章主要介紹了Java Swing 只關(guān)閉當(dāng)前窗體的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件
這篇文章主要介紹了Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件,下面文章圍繞FileUtils的相關(guān)資料展開怎么讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件的內(nèi)容,具有一定的參考價(jià)值,徐婭奧德小伙伴可以參考一下2021-12-12

