protected byte[] deriveEncryptionKey(PrivateKeyEncryptionContext encContext, int outputKeyLength) throws GeneralSecurityException { Objects.requireNonNull(encContext, "No encryption context"); ValidateUtils.checkNotNullAndNotEmpty(encContext.getCipherName(), "No cipher name"); ValidateUtils.checkNotNullAndNotEmpty(encContext.getCipherType(), "No cipher type"); ValidateUtils.checkNotNullAndNotEmpty(encContext.getCipherMode(), "No cipher mode"); byte[] initVector = Objects.requireNonNull(encContext.getInitVector(), "No encryption init vector"); ValidateUtils.checkTrue(initVector.length > 0, "Empty encryption init vector"); String password = ValidateUtils.checkNotNullAndNotEmpty(encContext.getPassword(), "No encryption password"); byte[] passBytes = password.getBytes(StandardCharsets.UTF_8); byte[] keyValue = new byte[outputKeyLength]; MessageDigest hash = SecurityUtils.getMessageDigest(BuiltinDigests.Constants.MD5); byte[] prevHash = GenericUtils.EMPTY_BYTE_ARRAY; for (int index = 0, remLen = keyValue.length; index < keyValue.length;) { hash.reset(); // just making sure hash.update(prevHash, 0, prevHash.length); hash.update(passBytes, 0, passBytes.length); hash.update(initVector, 0, Math.min(initVector.length, 8)); prevHash = hash.digest(); System.arraycopy(prevHash, 0, keyValue, index, Math.min(remLen, prevHash.length)); index += prevHash.length; remLen -= prevHash.length; } return keyValue; }
ValidateUtils.checkTrue(initVector.length > 0, "Empty encryption init vector"); String password = ValidateUtils.checkNotNullAndNotEmpty(encContext.getPassword(), "No encryption password"); byte[] passBytes = password.getBytes(StandardCharsets.UTF_8); byte[] prevHash = GenericUtils.EMPTY_BYTE_ARRAY;
@Override public int hashCode() { return GenericUtils.hashCode(getCipherName(), Boolean.TRUE) + GenericUtils.hashCode(getCipherType(), Boolean.TRUE) + GenericUtils.hashCode(getCipherMode(), Boolean.TRUE) + Objects.hashCode(getPassword()) + Arrays.hashCode(getInitVector()); }
@Override public int hashCode() { return GenericUtils.hashCode(getCipherName(), Boolean.TRUE) + GenericUtils.hashCode(getCipherType(), Boolean.TRUE) + GenericUtils.hashCode(getCipherMode(), Boolean.TRUE) + Objects.hashCode(getPassword()) + Arrays.hashCode(getInitVector()); }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (this == obj) { return true; } if (getClass() != obj.getClass()) { return false; } PrivateKeyEncryptionContext other = (PrivateKeyEncryptionContext) obj; return (GenericUtils.safeCompare(getCipherName(), other.getCipherName(), false) == 0) && (GenericUtils.safeCompare(getCipherType(), other.getCipherType(), false) == 0) && (GenericUtils.safeCompare(getCipherMode(), other.getCipherMode(), false) == 0) && (GenericUtils.safeCompare(getPassword(), other.getPassword(), true) == 0) && Arrays.equals(getInitVector(), other.getInitVector()); }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (this == obj) { return true; } if (getClass() != obj.getClass()) { return false; } PrivateKeyEncryptionContext other = (PrivateKeyEncryptionContext) obj; return (GenericUtils.safeCompare(getCipherName(), other.getCipherName(), false) == 0) && (GenericUtils.safeCompare(getCipherType(), other.getCipherType(), false) == 0) && (GenericUtils.safeCompare(getCipherMode(), other.getCipherMode(), false) == 0) && (GenericUtils.safeCompare(getPassword(), other.getPassword(), true) == 0) && Arrays.equals(getInitVector(), other.getInitVector()); }