-- 先建立外部模組

CREATE EXTENSION pgcrypto;

-- 再建立 test_user 表格

CREATE TABLE test_user(id SERIAL, username VARCHAR(32), password TEXT);

CREATE unique index idx_test_user_username on test_user using btree (username);

-- 插入純 MD5 加密,兩筆一樣結果會得到一樣的秘文

INSERT INTO test_user(username,password) VALUES ('user1', md5('123456'));

INSERT INTO test_user(username,password) VALUES ('user2', md5('123456'));

SELECT  * FROM test_user;

-- 插入 MD5 加鹽,兩筆一樣密碼,卻會產生不同結果,故更安全

INSERT INTO test_user(username,password) VALUES ('user3',crypt('123456', gen_salt('md5')));

INSERT INTO test_user(username,password) VALUES ('user4',crypt('123456', gen_salt('md5')));

SELECT * FROM test_user;

-- 插入bf 加密 ( 比 md5 更安全 (破解年限更長) ) : 

INSERT INTO test_user(username,password) VALUES ('user5',crypt('123456', gen_salt('bf')));

INSERT INTO test_user(username,password) VALUES ('user6',crypt('123456', gen_salt('bf')));

-- 查詢指定 username 與 password

SELECT * FROM test_user WHERE username ='user5' and password=crypt('123456',password);


arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(4) 人氣()