@Override public <A extends Appendable> A appendPrivateKeyEncryptionContext(A sb, PrivateKeyEncryptionContext encContext) throws IOException { if (encContext == null) { return sb; } sb.append("DEK-Info: ").append(encContext.getCipherName()) .append('-').append(encContext.getCipherType()) .append('-').append(encContext.getCipherMode()); byte[] initVector = encContext.getInitVector(); Objects.requireNonNull(initVector, "No encryption init vector"); ValidateUtils.checkTrue(initVector.length > 0, "Empty encryption init vector"); BufferUtils.appendHex(sb.append(','), BufferUtils.EMPTY_HEX_SEPARATOR, initVector); sb.append(System.lineSeparator()); return sb; }
@Override public PrivateKeyEncryptionContext clone() { try { PrivateKeyEncryptionContext copy = getClass().cast(super.clone()); byte[] v = copy.getInitVector(); if (v != null) { v = v.clone(); copy.setInitVector(v); } return copy; } catch (CloneNotSupportedException e) { // unexpected throw new RuntimeException("Failed to clone: " + toString()); } }
@Override public PrivateKeyEncryptionContext clone() { try { PrivateKeyEncryptionContext copy = getClass().cast(super.clone()); byte[] v = copy.getInitVector(); if (v != null) { v = v.clone(); copy.setInitVector(v); } return copy; } catch (CloneNotSupportedException e) { // unexpected throw new RuntimeException("Failed to clone: " + toString()); } }
@Override public <A extends Appendable> A appendPrivateKeyEncryptionContext( A sb, PrivateKeyEncryptionContext encContext) throws IOException { if (encContext == null) { return sb; } sb.append("DEK-Info: ").append(encContext.getCipherName()) .append('-').append(encContext.getCipherType()) .append('-').append(encContext.getCipherMode()); byte[] initVector = encContext.getInitVector(); Objects.requireNonNull(initVector, "No encryption init vector"); ValidateUtils.checkTrue(initVector.length > 0, "Empty encryption init vector"); BufferUtils.appendHex(sb.append(','), BufferUtils.EMPTY_HEX_SEPARATOR, initVector); sb.append(System.lineSeparator()); return sb; }
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.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");
ValidateUtils.checkTrue(keyValue.length > 0, "Empty encryption key"); byte[] initVector = Objects.requireNonNull(encContext.getInitVector(), "No encryption init vector"); ValidateUtils.checkTrue(initVector.length > 0, "Empty encryption init vector");
@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()); }
ValidateUtils.checkTrue(keyValue.length > 0, "Empty encryption key"); byte[] initVector = Objects.requireNonNull(encContext.getInitVector(), "No encryption init vector"); ValidateUtils.checkTrue(initVector.length > 0, "Empty encryption init vector");
@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()); }
protected byte[] applyPrivateKeyCipher(byte[] bytes, PrivateKeyEncryptionContext encContext, boolean encryptIt) throws GeneralSecurityException { String cipherName = encContext.getCipherName(); PrivateKeyObfuscator o = encContext.resolvePrivateKeyObfuscator(); if (o == null) { throw new NoSuchAlgorithmException("decryptPrivateKeyData(" + encContext + ")[encrypt=" + encryptIt + "] unknown cipher: " + cipherName); } if (encryptIt) { byte[] initVector = encContext.getInitVector(); if (GenericUtils.isEmpty(initVector)) { initVector = o.generateInitializationVector(encContext); encContext.setInitVector(initVector); } } return o.applyPrivateKeyCipher(bytes, encContext, encryptIt); } }
protected byte[] applyPrivateKeyCipher( byte[] bytes, PrivateKeyEncryptionContext encContext, boolean encryptIt) throws GeneralSecurityException, IOException { String cipherName = encContext.getCipherName(); PrivateKeyObfuscator o = encContext.resolvePrivateKeyObfuscator(); if (o == null) { throw new NoSuchAlgorithmException("decryptPrivateKeyData(" + encContext + ")[encrypt=" + encryptIt + "] unknown cipher: " + cipherName); } if (encryptIt) { byte[] initVector = encContext.getInitVector(); if (GenericUtils.isEmpty(initVector)) { initVector = o.generateInitializationVector(encContext); encContext.setInitVector(initVector); } } return o.applyPrivateKeyCipher(bytes, encContext, encryptIt); } }