001package com.avaje.ebean.config.dbplatform;
002
003/**
004 * Oracle encryption support.
005 * 
006 * <p>
007 * You will typically need to create your own encryption and decryption
008 * functions similar to the example ones below.
009 * </p>
010 * 
011 * <pre class="code">
012 * 
013 *  // Remember your DB user needs execute privilege on DBMS_CRYPTO 
014 *  // as well as your encryption and decryption functions
015 *  
016 *  
017 *  // This is an Example Encryption function only - please create your own.
018 * 
019 * CREATE OR REPLACE FUNCTION eb_encrypt(data IN VARCHAR, key in VARCHAR) RETURN RAW IS
020 * 
021 *     encryption_mode NUMBER := DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC  + DBMS_CRYPTO.PAD_PKCS5;
022 * 
023 *     BEGIN
024 *          RETURN DBMS_CRYPTO.ENCRYPT(UTL_I18N.STRING_TO_RAW (data, 'AL32UTF8'), 
025 *            encryption_mode, UTL_I18N.STRING_TO_RAW(key, 'AL32UTF8') );
026 *     END;
027 *     /
028 *     
029 *     
030 *     
031 *  // This is an Example Decryption function only - please create your own.
032 *     
033 * CREATE OR REPLACE FUNCTION eb_decrypt(data IN RAW, key IN VARCHAR) RETURN VARCHAR IS
034 * 
035 *     encryption_mode NUMBER := DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC  + DBMS_CRYPTO.PAD_PKCS5;
036 * 
037 *     BEGIN
038 *          RETURN UTL_RAW.CAST_TO_VARCHAR2(DBMS_CRYPTO.DECRYPT
039 *            (data, encryption_mode, UTL_I18N.STRING_TO_RAW(key, 'AL32UTF8')));
040 *     END;
041 *     /
042 * </pre>
043 * 
044 * @author rbygrave
045 */
046public class OracleDbEncrypt extends AbstractDbEncrypt {
047
048  /**
049   * Constructs the Oracle10DbEncrypt with default encrypt and decrypt stored procedures.
050   */
051  public OracleDbEncrypt() {
052    this("eb_encrypt", "eb_decrypt");
053  }
054
055  /**
056   * Constructs the Oracle10DbEncrypt specifying encrypt and decrypt stored procedures.
057   *
058   * @param encryptFunction the encrypt stored procedure
059   * @param decryptFunction the decrypt stored procedure
060   */
061  public OracleDbEncrypt(String encryptFunction, String decryptFunction) {
062
063    this.varcharEncryptFunction = new OraVarcharFunction(encryptFunction, decryptFunction);
064    this.dateEncryptFunction = new OraDateFunction(encryptFunction, decryptFunction);
065  }
066
067  /**
068   * VARCHAR encryption/decryption function.
069   */
070  private static class OraVarcharFunction implements DbEncryptFunction {
071
072    private final String encryptfunction;
073    private final String decryptfunction;
074
075    public OraVarcharFunction(String encryptfunction, String decryptfunction) {
076      this.encryptfunction = encryptfunction;
077      this.decryptfunction = decryptfunction;
078    }
079
080    public String getDecryptSql(String columnWithTableAlias) {
081      return decryptfunction + "(" + columnWithTableAlias + ",?)";
082    }
083
084    public String getEncryptBindSql() {
085      return encryptfunction + "(?,?)";
086    }
087
088  }
089
090  /**
091   * DATE encryption/decryption function.
092   */
093  private static class OraDateFunction implements DbEncryptFunction {
094
095    private final String encryptfunction;
096    private final String decryptfunction;
097
098    public OraDateFunction(String encryptfunction, String decryptfunction) {
099      this.encryptfunction = encryptfunction;
100      this.decryptfunction = decryptfunction;
101    }
102
103    public String getDecryptSql(String columnWithTableAlias) {
104      return "to_date(" + decryptfunction + "(" + columnWithTableAlias + ",?),'YYYYMMDD')";
105    }
106
107    public String getEncryptBindSql() {
108      return encryptfunction + "(to_char(?,'YYYYMMDD'),?)";
109    }
110
111  }
112}