Skip to content

Instantly share code, notes, and snippets.

@ifukazoo
Last active September 3, 2017 08:00
Show Gist options
  • Save ifukazoo/3b78aad7b3e481138ff26a4a978dad2c to your computer and use it in GitHub Desktop.
Save ifukazoo/3b78aad7b3e481138ff26a4a978dad2c to your computer and use it in GitHub Desktop.
カラムの暗号化
create table enctest (
id serial not null primary key,
password bytea not null,
key bytea not null
)
;
select
convert_from(
decrypt(
-- 暗号化したデータ
password
-- 暗号化した暗号化鍵
,decrypt(key, digest('kek','sha1'), 'aes')
,'aes')
,'UTF-8')
from
enctest
where
id = 5
;
create extension pgcrypto;
insert into enctest
select
nextval('enctest_id_seq')
-- データの暗号化
,encrypt(
convert_to('こんにちは 世界。','UTF-8')
,g.key
,'aes')
-- 暗号化鍵の暗号化
,encrypt(
g.key
,digest('kek','sha1')
,'aes')
from
-- 暗号化鍵の生成
(select gen_random_bytes(128) as key) as g
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment