@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; }
public PrivateKeyObfuscator resolvePrivateKeyObfuscator() { PrivateKeyObfuscator value = getPrivateKeyObfuscator(); if (value != null) { return value; } return getRegisteredPrivateKeyObfuscator(getCipherName()); }
public PrivateKeyObfuscator resolvePrivateKeyObfuscator() { PrivateKeyObfuscator value = getPrivateKeyObfuscator(); if (value != null) { return value; } return getRegisteredPrivateKeyObfuscator(getCipherName()); }
@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; }
public static final PrivateKeyEncryptionContext resolveEffectiveContext(PrivateKeyEncryptionContext encContext) { if (encContext == null) { return null; } String cipherName = encContext.getCipherName(); String cipherType = encContext.getCipherType(); PrivateKeyEncryptionContext effContext = encContext; if ("EDE3".equalsIgnoreCase(cipherType)) { cipherName += "ede"; effContext = encContext.clone(); effContext.setCipherName(cipherName); } return effContext; } }
public static final PrivateKeyEncryptionContext resolveEffectiveContext(PrivateKeyEncryptionContext encContext) { if (encContext == null) { return null; } String cipherName = encContext.getCipherName(); String cipherType = encContext.getCipherType(); PrivateKeyEncryptionContext effContext = encContext; if ("EDE3".equalsIgnoreCase(cipherType)) { cipherName += "ede"; effContext = encContext.clone(); effContext.setCipherName(cipherName); } return effContext; } }
@Override public String toString() { return GenericUtils.join(new String[]{getCipherName(), getCipherType(), getCipherMode()}, '-'); }
@Override public String toString() { return GenericUtils.join(new String[]{getCipherName(), getCipherType(), getCipherMode()}, '-'); }
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; }
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");
protected byte[] applyPrivateKeyCipher(byte[] bytes, PrivateKeyEncryptionContext encContext, int numBits, byte[] keyValue, boolean encryptIt) throws GeneralSecurityException { Objects.requireNonNull(encContext, "No encryption context"); String cipherName = ValidateUtils.checkNotNullAndNotEmpty(encContext.getCipherName(), "No cipher name"); ValidateUtils.checkNotNullAndNotEmpty(encContext.getCipherType(), "No cipher type"); String cipherMode = ValidateUtils.checkNotNullAndNotEmpty(encContext.getCipherMode(), "No cipher mode");
@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()); }
throws IOException, GeneralSecurityException { Objects.requireNonNull(encContext, "No encryption context"); String cipherName = ValidateUtils.checkNotNullAndNotEmpty(encContext.getCipherName(), "No cipher name"); ValidateUtils.checkNotNullAndNotEmpty(encContext.getCipherType(), "No cipher type"); String cipherMode = ValidateUtils.checkNotNullAndNotEmpty(encContext.getCipherMode(), "No cipher mode");
@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); } }