/** * Get an cipher given a name * @param name the cipher name * @return the cipher, or null if a suitable one could not be found */ public static Cipher getCipher(Configuration conf, String name) { return getCipherProvider(conf).getCipher(name); }
/** * Get names of supported encryption algorithms * * @return Array of strings, each represents a supported encryption algorithm */ public static String[] getSupportedCiphers(Configuration conf) { return getCipherProvider(conf).getSupportedCiphers(); }
/** * Encrypt a stream of plaintext given a context and IV * @param out ciphertext * @param in plaintet * @param context * @param iv * @throws IOException */ public static void encrypt(OutputStream out, InputStream in, Context context, byte[] iv) throws IOException { Encryptor e = context.getCipher().getEncryptor(); e.setKey(context.getKey()); e.setIv(iv); // can be null e.reset(); encrypt(out, in, e); }
/** * Decrypt a stream of ciphertext given a context and IV * @param out * @param in * @param outLen * @param context * @param iv * @throws IOException */ public static void decrypt(OutputStream out, InputStream in, int outLen, Context context, byte[] iv) throws IOException { Decryptor d = context.getCipher().getDecryptor(); d.setKey(context.getKey()); d.setIv(iv); // can be null decrypt(out, in, outLen, d); }
@Override public OutputStream createEncryptionStream(OutputStream out, Context context, byte[] iv) throws IOException { Preconditions.checkNotNull(context); Preconditions.checkState(context.getKey() != null, "Context does not have a key"); Preconditions.checkNotNull(iv); Encryptor e = getEncryptor(); e.setKey(context.getKey()); e.setIv(iv); return e.createEncryptionStream(out); }
@Override public OutputStream createEncryptionStream(OutputStream out, Context context, byte[] iv) throws IOException { Preconditions.checkNotNull(context); Preconditions.checkState(context.getKey() != null, "Context does not have a key"); Preconditions.checkNotNull(iv); Encryptor e = getEncryptor(); e.setKey(context.getKey()); e.setIv(iv); return e.createEncryptionStream(out); }
@Override public InputStream createDecryptionStream(InputStream in, Context context, byte[] iv) throws IOException { Preconditions.checkNotNull(context); Preconditions.checkState(context.getKey() != null, "Context does not have a key"); Preconditions.checkNotNull(iv); Decryptor d = getDecryptor(); d.setKey(context.getKey()); d.setIv(iv); return d.createDecryptionStream(in); }
protected void incrementIv(int v) { Encryption.incrementIv(iv.get(), 1 + (v / encryptor.getBlockSize())); }
@Override public OutputStream createEncryptionStream(OutputStream out, Encryptor encryptor) throws IOException { return encryptor.createEncryptionStream(out); }
public static Context newContext() { return new Context(); }
@Override public Key[] getKeys(String[] aliases) { Key[] result = new Key[aliases.length]; for (int i = 0; i < aliases.length; i++) { result[i] = getKey(aliases[i]); } return result; }
@Override public Context setKey(Key key) { super.setKey(key); return this; }
public static DefaultCipherProvider getInstance() { if (instance != null) { return instance; } instance = new DefaultCipherProvider(); return instance; }
@Override public Context setCipher(Cipher cipher) { super.setCipher(cipher); return this; }
public EncryptedKvDecoder(InputStream in, Decryptor decryptor) { super(in); this.decryptor = decryptor; if (decryptor != null) { this.iv = new byte[decryptor.getIvLength()]; } }
public static CryptoCipherProvider getInstance() { if (instance != null) { return instance; } instance = new CryptoCipherProvider(); return instance; }
/** * Encrypt a block of plaintext * @param out ciphertext * @param src plaintext * @param offset * @param length * @param context * @param iv * @throws IOException */ public static void encrypt(OutputStream out, byte[] src, int offset, int length, Context context, byte[] iv) throws IOException { Encryptor e = context.getCipher().getEncryptor(); e.setKey(context.getKey()); e.setIv(iv); // can be null e.reset(); encrypt(out, src, offset, length, e); }
/** * Decrypt a block of ciphertext from a stream given a context and IV * @param dest * @param destOffset * @param in * @param destSize * @param context * @param iv * @throws IOException */ public static void decrypt(byte[] dest, int destOffset, InputStream in, int destSize, Context context, byte[] iv) throws IOException { Decryptor d = context.getCipher().getDecryptor(); d.setKey(context.getKey()); d.setIv(iv); // can be null decrypt(dest, destOffset, in, destSize, d); }
public static Context newContext(Configuration conf) { return new Context(conf); }