public static void testEncryption(final Configuration conf, final String cipher, byte[] key)
    throws IOException {
    if (cipher == null) {
      return;
    }
    if (!Encryption.isEncryptionEnabled(conf)) {
      String message =
        String.format("Cipher %s failed test: encryption is disabled on the cluster", cipher);
      throw new IOException(message);
    }
    testKeyProvider(conf);
    testCipherProvider(conf);
    Boolean result = cipherResults.get(cipher);
    if (result == null) {
      try {
        Encryption.Context context = Encryption.newContext(conf);
        context.setCipher(Encryption.getCipher(conf, cipher));
        if (key == null) {
          // Make a random key since one was not provided
          context.setKey(context.getCipher().getRandomKey());
        } else {
          // This will be a wrapped key from schema
          context.setKey(EncryptionUtil.unwrapKey(conf,
            conf.get(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase"), key));
        }
        byte[] iv = null;
        if (context.getCipher().getIvLength() > 0) {
          iv = new byte[context.getCipher().getIvLength()];
          Bytes.secureRandom(iv);
        }
        byte[] plaintext = new byte[1024];
        Bytes.random(plaintext);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Encryption.encrypt(out, new ByteArrayInputStream(plaintext), context, iv);
        byte[] ciphertext = out.toByteArray();
        out.reset();
        Encryption.decrypt(out, new ByteArrayInputStream(ciphertext), plaintext.length, context,
          iv);
        byte[] test = out.toByteArray();
        if (!Bytes.equals(plaintext, test)) {
          throw new IOException("Did not pass encrypt/decrypt test");
        }
        cipherResults.put(cipher, true);
      } catch (Exception e) {
        cipherResults.put(cipher, false);
        throw new IOException("Cipher " + cipher + " failed test: " + e.getMessage(), e);
      }
    } else if (!result) {
      throw new IOException("Cipher " + cipher + " previously failed test");
    }
  }
Check that the specified cipher can be loaded and initialized, or throw an exception. Verifies
key and cipher provider configuration as a prerequisite for cipher verification. Also verifies
if encryption is enabled globally.
@param conf   HBase configuration
@param cipher chiper algorith to use for the column family
@param key    encryption key
@throws IOException in case of encryption configuration error
hbase-server/src/main/java/org/apache/hadoop/hbase/util/EncryptionTest.java