/** * Encrypts the given String via SHA crypt algorithm. * * @param s * @return */ public static String digest(final String s) { return encode(s, "SHA"); }
@Override public String encryptParameterMessage(String message) { return Crypt.encrypt(StringUtils.rightPad(configService.getTeamCalCryptPassword(), 32, "x"), message); }
@Override public Map<String, String> decryptParameterMessage(String message) { Map<String, String> result = new HashMap<>(); String decryptMessage = Crypt.decrypt(StringUtils.rightPad(configService.getTeamCalCryptPassword(), 32, "x"), message); if (decryptMessage != null) { String[] parametersAndValues = decryptMessage.split("&"); for (String parameterWithValue : parametersAndValues) { String[] parameterAndValue = parameterWithValue.split("="); if (parameterAndValue.length > 1) { result.put(parameterAndValue[0], parameterAndValue[1]); } } } return result; }
/** * Encrypts the given str with AES. The password is first converted using SHA-256. * * @param password * @param str * @return The base64 encoded result (url safe). */ public static String encrypt(final String password, final String data) { initialize(); try { // AES is sometimes not part of Java, therefore use bouncy castle provider: final Cipher cipher = Cipher.getInstance(CRYPTO_ALGORITHM); final byte[] keyValue = getPassword(password); final Key key = new SecretKeySpec(keyValue, "AES"); cipher.init(Cipher.ENCRYPT_MODE, key); final byte[] encVal = cipher.doFinal(data.getBytes("UTF-8")); final String encryptedValue = Base64.encodeBase64URLSafeString(encVal); return encryptedValue; } catch (final Exception ex) { log.error("Exception encountered while trying to encrypt with Algorithm 'AES' and the given password: " + ex.getMessage(), ex); return null; } }
/** * Encrypts the given str with AES. The key is the current authenticationToken of the given user (by id) (first 16 * bytes of it). * * @param userId * @param data * @return The base64 encoded result (url safe). * @see Crypt#encrypt(String, String) */ public String encrypt(final Integer userId, final String data) { final String authenticationToken = StringUtils.rightPad(getCachedAuthenticationToken(userId), 32, "x"); return Crypt.encrypt(authenticationToken, data); }
/** * @param userId * @param encryptedString * @return The decrypted string. * @see Crypt#decrypt(String, String) */ @Override public String decrypt(final Integer userId, final String encryptedString) { // final PFUserDO user = userCache.getUser(userId); // for faster access (due to permanent usage e. g. by subscription of calendars // (ics). final String authenticationToken = StringUtils.rightPad(getCachedAuthenticationToken(userId), 32, "x"); return Crypt.decrypt(authenticationToken, encryptedString); }
/** * @param password * @param encryptedString * @return */ public static String decrypt(final String password, final String encryptedString) { initialize(); try { final Cipher cipher = Cipher.getInstance(CRYPTO_ALGORITHM); final byte[] keyValue = getPassword(password); final Key key = new SecretKeySpec(keyValue, "AES"); cipher.init(Cipher.DECRYPT_MODE, key); final byte[] decordedValue = Base64.decodeBase64(encryptedString); final byte[] decValue = cipher.doFinal(decordedValue); final String decryptedValue = new String(decValue, "UTF-8"); return decryptedValue; } catch (BadPaddingException bpe) { log.warn(bpe.getMessage()); return null; } catch (final Exception ex) { log.error("Exception encountered while trying to encrypt with Algorithm 'AES' and the given password: " + ex.getMessage(), ex); return null; } }
/** * Encrypts the password with a new generated salt string and the pepper string if configured any. * * @param user The user to user. * @param password as clear text. * @see Crypt#digest(String) */ @Override public void createEncryptedPassword(final PFUserDO user, final String password) { final String saltString = createSaltString(); user.setPasswordSalt(saltString); final String encryptedPassword = Crypt.digest(getPepperString() + saltString + password); user.setPassword(encryptedPassword); }
public static String digest(final String s, final String alg) { return encode(s, alg); }
public static boolean check(final String pass, final String encoded) { final String alg = encoded.substring(0, encoded.indexOf('{')); return encoded.equals(encode(pass, alg)); }