001package com.avaje.ebean.config.dbplatform;
002
003/**
004 * H2 encryption support via encrypt decrypt function.
005 * 
006 * @author rbygrave
007 */
008public class H2DbEncrypt extends AbstractDbEncrypt {
009
010  public H2DbEncrypt() {
011    this.varcharEncryptFunction = new H2VarcharFunction();
012    this.dateEncryptFunction = new H2DateFunction();
013  }
014
015  /**
016   * For H2 encrypt function returns false binding the key before the data.
017   */
018  public boolean isBindEncryptDataFirst() {
019    return false;
020  }
021
022  private static class H2VarcharFunction implements DbEncryptFunction {
023
024    public String getDecryptSql(String columnWithTableAlias) {
025      // Hmmm, this looks ugly - checking with H2 Database folks.
026      return "TRIM(CHAR(0) FROM UTF8TOSTRING(DECRYPT('AES', STRINGTOUTF8(?), "
027          + columnWithTableAlias + ")))";
028    }
029
030    public String getEncryptBindSql() {
031      return "ENCRYPT('AES', STRINGTOUTF8(?), STRINGTOUTF8(?))";
032    }
033
034  }
035
036  private static class H2DateFunction implements DbEncryptFunction {
037
038    public String getDecryptSql(String columnWithTableAlias) {
039      return "PARSEDATETIME(TRIM(CHAR(0) FROM UTF8TOSTRING(DECRYPT('AES', STRINGTOUTF8(?), "
040          + columnWithTableAlias + "))),'yyyyMMdd')";
041    }
042
043    public String getEncryptBindSql() {
044      return "ENCRYPT('AES', STRINGTOUTF8(?), STRINGTOUTF8(FORMATDATETIME(?,'yyyyMMdd')))";
045    }
046
047  }
048}