Last active
September 3, 2017 08:00
-
-
Save ifukazoo/3b78aad7b3e481138ff26a4a978dad2c to your computer and use it in GitHub Desktop.
カラムの暗号化
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table enctest ( | |
id serial not null primary key, | |
password bytea not null, | |
key bytea not null | |
) | |
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
convert_from( | |
decrypt( | |
-- 暗号化したデータ | |
password | |
-- 暗号化した暗号化鍵 | |
,decrypt(key, digest('kek','sha1'), 'aes') | |
,'aes') | |
,'UTF-8') | |
from | |
enctest | |
where | |
id = 5 | |
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create extension pgcrypto; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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