C++,python,热爱算法和机器学习
全部博文(1214)
分类: 网络与安全
2014-04-11 17:39:00
今天有同事问我如何给数据库中的敏感数据加密问题,开始没相到什么方法,后来查了下手册,可以通过
PostgreSQL 的 pgcrypto 模块实现,下面是实验步骤:
一 环境准备
--1.1 创建外部模块 pgcrypto
mydb=# create extension pgcrypto ; CREATE EXTENSION |
--1.2 创建测试表
mydb=> create table test_user(id serial,username varchar(32),password text); NOTICE: CREATE TABLE will create implicit sequence "test_user_id_seq" for serial column "test_user.id" CREATE TABLE
mydb=> create unique index idx_test_user_username on test_user using btree (username);
mydb=> \d test_user |
mydb=> insert into test_user(username,password) values ('user1',md5('123456')); INSERT 0 1 mydb=> insert into test_user(username,password) values ('user2',md5('123456')); INSERT 0 1 |
--2.2 查询用户信息,解密
mydb=> select * From test_user; id | username | password ----+----------+---------------------------------- 1 | user1 | e10adc3949ba59abbe56e057f20f883e 2 | user2 | e10adc3949ba59abbe56e057f20f883e (2 rows)
mydb=> select * From test_user where password=md5('123456'); |
mydb=> insert into test_user(username,password) values ('user3',crypt('123456',gen_salt('md5'))); INSERT 0 1 mydb=> insert into test_user(username,password) values ('user4',crypt('123456',gen_salt('md5'))); INSERT 0 1 |
--3.2 查询新增用户信息
mydb=> select * From test_user where username in ('user3','user4'); id | username | password ----+----------+------------------------------------ 5 | user3 | $1$cS7Bs67A$5c2FTClGTOBYiHpG1HyvA/ 6 | user4 | $1$L6Rao5/l$7URcaCbT9Hrsrt9JcoBGq. (2 rows) |
备注:虽然 user3,user4 使用相同的密码,但经过 crypt() 函数加密后,加密后的密码值不同,
显然,这种加密方式要比 md5 安全。
--3.3 查询,解密测试
mydb=> select * From test_user where username ='user3' and password=crypt('123456',password); id | username | password ----+----------+------------------------------------ 5 | user3 | $1$cS7Bs67A$5c2FTClGTOBYiHpG1HyvA/ (1 row)
mydb=> select * From test_user where username ='user4' and password=crypt('123456',password); |
四附:函数
--4.1 crypt()
crypt(password text, salt text) returns text
Calculates a crypt(3)-style hash of password. When storing a new password, you need to use gen_salt() to generate a new salt value. To check a password, pass the stored hash value as salt, and test whether the result matches the stored value.
--4.2 crypt() 函数支持的加密算法
--4.3 gen_salt()
gen_salt(type text [, iter_count integer ]) returns text
Generates a new random salt string for use in crypt(). The salt string also tells crypt() which algorithm to use.
The type parameter specifies the hashing algorithm. The accepted types are: des, xdes, md5 and bf.