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

The following examples show how to use org.springframework.security.crypto.keygen.KeyGenerators. These examples are extracted from open source projects. 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 Project: SMSC   Source File: EncrypterUtil.java    License: Apache License 2.0 6 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;
}
 
Example 2
Source Project: paascloud-master   Source File: MD5Test.java    License: Apache License 2.0 5 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 3
Source Project: SMSC   Source File: EncrypterUtilEncryptionTest.java    License: 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 4
/**
 * 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 5
Source Project: lemon   Source File: MessageDigestPasswordEncoder.java    License: 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 6
/**
 * 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 7
/**
 * 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));
}