@Override public Pair<ReturnResult, APIUserEntry> newAPIUser(NewAPIUserRequest request) { if(StringUtils.isBlank(request.getName()) || StringUtils.isBlank(request.getRole())) { return new ImmutablePair<>(ReturnResult.PARAMETERS_MISSING, null); } APIUserEntity entity = new APIUserEntity(); entity.setLastAccess(new LocalDateTime()); entity.setName(request.getName()); entity.setCreatedBy(AuthenticationUtil.getUserName()); entity.setRole(request.getRole()); // Let's create the secret for signatures. This is going to be a bit more complex than public key String secret = (new LocalDateTime()).toString()+entity.getName()+entity.getCreatedBy()+entity.getRole(); String secCrypt = Sha2Crypt.sha512Crypt(secret.getBytes(), "$6$0$"); secret = new String(Base64.encodeBase64(secCrypt.substring(5).getBytes(), false, true)); entity.setSecret(secret); em.persist(entity); return new ImmutablePair<>(ReturnResult.OPERATION_SUCCESSFUL, formAPIUserEntry(entity)); }
public PasswordEncrypt(final String key) { final byte[] keyBytes = key.getBytes(US_ASCII); this.md5 = Md5Crypt.md5Crypt(keyBytes.clone()); this.apr1 = Md5Crypt.apr1Crypt(keyBytes.clone()); this.sha256 = Sha2Crypt.sha256Crypt(keyBytes.clone()); this.sha512 = Sha2Crypt.sha512Crypt(keyBytes.clone()); Arrays.fill(keyBytes, (byte) 0); }
public static boolean checkPassword(final String crypted, final String key) { String crypted2 = null; if (crypted == null) return false; if (crypted.length() < 24) return false; if (crypted.charAt(0) != '$') return false; final int offset2ndDolar = crypted.indexOf('$', 1); if (offset2ndDolar < 0) return false; final int offset3ndDolar = crypted.indexOf('$', offset2ndDolar + 1); if (offset3ndDolar < 0) return false; final String salt = crypted.substring(0, offset3ndDolar + 1); final byte[] keyBytes = key.getBytes(US_ASCII); if (crypted.startsWith("$1$")) { // MD5 crypted2 = Md5Crypt.md5Crypt(keyBytes.clone(), salt); } else if (crypted.startsWith("$apr1$")) { // APR1 crypted2 = Md5Crypt.apr1Crypt(keyBytes.clone(), salt); } else if (crypted.startsWith("$5$")) { // SHA2-256 crypted2 = Sha2Crypt.sha256Crypt(keyBytes.clone(), salt); } else if (crypted.startsWith("$6$")) { // SHA2-512 crypted2 = Sha2Crypt.sha512Crypt(keyBytes.clone(), salt); } Arrays.fill(keyBytes, (byte) 0); if (crypted2 == null) return false; return crypted.equals(crypted2); }
@NonNull @Override public String encode(@NonNull String text) { return Sha2Crypt.sha256Crypt(text.getBytes()); }
@NonNull @Override public String encode(@NonNull String text) { return Sha2Crypt.sha512Crypt(text.getBytes()); }