org.springframework.security.crypto.keygen.KeyGenerators Java Examples

The following examples show how to use org.springframework.security.crypto.keygen.KeyGenerators. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MD5Test.java    From paascloud-master with Apache License 2.0 1230 votes vote down vote up
/**
 * The entry point of application.
 *
 * @param args the input arguments
 *
 */
public static void main(String[] args) {
	md5(); // 使用简单的MD5加密方式     

	sha_256(); // 使用256的哈希算法(SHA)加密     

	sha_SHA_256(); // 使用SHA-256的哈希算法(SHA)加密     

	md5_SystemWideSaltSource(); // 使用MD5再加全局加密盐加密的方式加密
	String salt = KeyGenerators.string().generateKey();
	log.info(salt);
	log.info("salt.length={}", salt.length());
	String encrypt = Md5Util.encrypt("123456");
	log.info(encrypt);
}
 
Example #2
Source File: EncrypterUtilEncryptionTest.java    From SMSC with Apache License 2.0 5 votes vote down vote up
@Before
public void removeCryptographyRestriction() throws Exception {
    removeCryptographyRestrictions();

    Whitebox.setInternalState(EncrypterUtil.class, "smsc.io");

    obj1 = new EncryptionTestClassWithPrivateFieldWithoutSalt();
    obj1.setFieldToBeEncoded(STRING_FOR_ENCODING);
    obj2 = new EncryptionTestClassWithPublicFieldAndSalt();
    obj2.setFieldToBeEncoded(STRING_FOR_ENCODING);
    obj2.setSalt(KeyGenerators.string().generateKey());
}
 
Example #3
Source File: DefaultTextEncryptor.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns a Base64 string composed by the salt followed by the encrypted
 * data.
 */
@Override
public String encrypt(String plainText) {

    // default StringKeyGenerator returns a 8 bytes hex-encoded string
    String salt = KeyGenerators.string().generateKey();

    BytesEncryptor encryptor = Encryptors.standard(key, salt);
    byte[] encrypted = encryptor.encrypt(plainText.getBytes());

    byte[] saltAndSecret = ArrayUtils.addAll(Hex.decode(salt), encrypted);
    return Base64.getEncoder().encodeToString(saltAndSecret);
}
 
Example #4
Source File: MessageDigestPasswordEncoder.java    From lemon with Apache License 2.0 5 votes vote down vote up
public MessageDigestPasswordEncoder(String algorithm, CharSequence secret) {
    try {
        messageDigest = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("No such hashing algorithm", e);
    }

    this.secret = Utf8.encode(secret);
    this.saltGenerator = KeyGenerators.secureRandom();
}
 
Example #5
Source File: AESGCMSymmetricEncryptionFactory.java    From flair-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiate symmetric cipher
 *
 * @param secretKey secret key represented in byte array
 * @return instance of {@link BytesEncryptor}
 */
@Override
public BytesEncryptor getSymmetricEncryption(byte[] secretKey) {
    return new AESGCMBytesEncryptor(secretKey, KeyGenerators.secureRandom(16));
}
 
Example #6
Source File: AESGCMSymmetricEncryptionFactory.java    From flair-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiate symmetric cipher
 *
 * @param passphrase password used for deriving key
 * @param salt       salt used for deriving key
 * @return instance of {@link BytesEncryptor}
 */
@Override
public BytesEncryptor getSymmetricEncryption(String passphrase, CharSequence salt, KeyDerivationFunction kdf) {
    return new AESGCMBytesEncryptor(passphrase, kdf, salt, KeyGenerators.secureRandom(16));
}
 
Example #7
Source File: EncrypterUtil.java    From SMSC with Apache License 2.0 -32 votes vote down vote up
/**
 * Additional method to get salt from object.
 *
 * @param obj entity object
 * @return string with salt
 */
private static CharSequence getSalt(Object obj) throws IllegalAccessException {
    CharSequence salt;
    try {
        Field saltField = obj.getClass().getDeclaredField("salt");
        saltField.setAccessible(true);
        salt = (CharSequence) saltField.get(obj);

        if (salt == null || salt.toString().isEmpty()) {
            salt = KeyGenerators.string().generateKey();
            saltField.set(obj, salt);
        }

        saltField.setAccessible(false);

    } catch (NoSuchFieldException e) {
        LOGGER.info("Decrypt exception: salt field not found.");
        // Use encoded class name as salt, if salt field not available.
        salt = new String(Hex.encode(obj.getClass().getName().getBytes()));
    }

    return salt;
}