publicclassBcryptCipher{// generate salt seedprivatestaticfinalint SALT_SEED =12;// the head fo saltprivatestaticfinal String SALT_STARTSWITH ="$2a$12";publicstaticfinal String SALT_KEY ="salt";publicstaticfinal String CIPHER_KEY ="cipher";/**
* Bcrypt encryption algorithm method
* @param encryptSource
* need to encrypt the string
* @return Map , two values in Map , salt and cipher
*/publicstatic Map<String, String>Bcrypt(final String encryptSource){
String salt = BCrypt.gensalt(SALT_SEED);
Map<String, String> bcryptResult =Bcrypt(salt, encryptSource);return bcryptResult;}/**
*
* @param salt encrypt salt, Must conform to the rules
* @param encryptSource
* @return
*/publicstatic Map<String, String>Bcrypt(final String salt,final String encryptSource){if(StringUtils.isBlank(encryptSource)){thrownewRuntimeException("Bcrypt encrypt input params can not be empty");}if(StringUtils.isBlank(salt)|| salt.length()!=29){thrownewRuntimeException("Salt can't be empty and length must be to 29");}if(!salt.startsWith(SALT_STARTSWITH)){thrownewRuntimeException("Invalid salt version, salt version is $2a$12");}
String cipher = BCrypt.hashpw(encryptSource, salt);
Map<String, String> bcryptResult =newHashMap<String, String>();
bcryptResult.put(SALT_KEY, salt);
bcryptResult.put(CIPHER_KEY, cipher);return bcryptResult;}}