Thanks - just what I was after. I haven't had a chance to try it yet but I'm sure that will be just what I was after.
Thanks, Rob. On Jan 26, 8:22 am, Thomas Mueller <[email protected]> wrote: > Hi, > > > I am wondering if/how I can make the syntax cleaner. To be closer to > > that of Postgres pgp_sym_decrypt, MySql aes_decrypt etc. > > I don't know exactly how pgp_sym_decrypt and aes_decrypt work, so 'my' > solution is probably not compatible. In my solution, the key is hashed > before it's used as the encryption key. > > CALL CAST(ENCRYPT('AES', HASH('SHA256', STRINGTOUTF8('key'), 1), > STRINGTOUTF8('Hello')) AS VARCHAR); > > CALL TRIM(CHAR(0) FROM UTF8TOSTRING(DECRYPT('AES', HASH('SHA256', > STRINGTOUTF8('key'), 1), '16e44604230717eec9f5fa6058e77e83'))); > > This is quite verbose. Here are Java functions: > > DROP ALIAS ENC; > DROP ALIAS DEC; > CREATE ALIAS ENC AS $$ > import org.h2.security.*; > import org.h2.util.*; > @CODE > String encrypt(String data, String key) throws Exception { > byte[] k = new SHA256().getHash(key.getBytes("UTF-8"), false); > byte[] b1 = data.getBytes("UTF-8"); > byte[] buff = new byte[(b1.length + 15) / 16 * 16]; > System.arraycopy(b1, 0, buff, 0, b1.length); > BlockCipher bc = CipherFactory.getBlockCipher("AES"); > bc.setKey(k); > bc.encrypt(buff, 0, buff.length); > return ByteUtils.convertBytesToString(buff);} > > $$; > CREATE ALIAS DEC AS $$ > import org.h2.security.*; > import org.h2.util.*; > @CODE > String decrypt(String data, String key) throws Exception { > byte[] k = new SHA256().getHash(key.getBytes("UTF-8"), false); > byte[] buff = ByteUtils.convertStringToBytes(data); > BlockCipher bc = CipherFactory.getBlockCipher("AES"); > bc.setKey(k); > bc.decrypt(buff, 0, buff.length); > return StringUtils.trim(new String(buff, "UTF-8"), false, true, > "\u0000");} > > $$; > CALL ENC('Hello', 'key'); > CALL DEC(ENC('Hello', 'key'), 'key'); > > > Are there any plans to provide easier/more DECRYPT functions in the > > future? > > I will add the code above to the src/test/org/h2/test/todo/tools.sql, > but I will currently not add the methods to the database engine > itself. > > Regards, > Thomas -- You received this message because you are subscribed to the Google Groups "H2 Database" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.
