-
Notifications
You must be signed in to change notification settings - Fork 1
/
arc_crypt.sql
64 lines (58 loc) · 2 KB
/
arc_crypt.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
DELIMITER //
select '' into @msg//
DROP FUNCTION IF EXISTS ARC_XORSTR //
CREATE FUNCTION ARC_XORSTR (s CHAR(128),c SMALLINT) RETURNS CHAR(128) DETERMINISTIC
BEGIN
DECLARE result CHAR(129) DEFAULT "";
DECLARE i SMALLINT DEFAULT 1;
DECLARE l SMALLINT DEFAULT 128;
SET result = LPAD('',129,'-');
WHILE (i <= l) DO
SET result = INSERT(result, i, 1, CHAR(ASCII(SUBSTRING(s,i,1))^c));
SET i = i + 1;
END WHILE;
RETURN SUBSTRING(result,1,128);
END//
DROP FUNCTION IF EXISTS ARC_XORSTRS //
CREATE FUNCTION ARC_XORSTRS (s CHAR(64),c CHAR(64)) RETURNS CHAR(64) DETERMINISTIC
BEGIN
DECLARE i SMALLINT DEFAULT 1;
DECLARE l SMALLINT DEFAULT 0;
DECLARE chr SMALLINT DEFAULT 0;
DECLARE result CHAR(65) DEFAULT "";
SET result = LPAD('',65,'-');
SET l = LENGTH(s);
IF LENGTH(c) < l THEN
SET l = LENGTH(c);
END IF;
WHILE (i <= l) DO
SET chr = ASCII(SUBSTRING(s,i,1)) ^ ASCII(SUBSTRING(c,i,1));
SET result = INSERT(result, i, 1, CHAR(chr));
SET i = i + 1;
END WHILE;
RETURN SUBSTRING(result,1,64);
END//
DROP FUNCTION IF EXISTS ARC_HMAC_SHA_512 //
CREATE FUNCTION ARC_HMAC_SHA_512 (saltkey CHAR(128),passwd CHAR(128)) RETURNS CHAR(64) DETERMINISTIC
BEGIN
DECLARE result CHAR(64) DEFAULT "";
SET result = UNHEX(SHA2(CONCAT(ARC_XORSTR(passwd,54),saltkey),512));
SET result = UNHEX(SHA2(CONCAT(ARC_XORSTR(passwd,92),result),512));
RETURN result;
END//
DROP FUNCTION IF EXISTS ARC_PBKDF2 //
CREATE FUNCTION ARC_PBKDF2 (saltkey CHAR(64),passwd CHAR(64),l INT) RETURNS CHAR(64) DETERMINISTIC
BEGIN
DECLARE prevhash CHAR(64) DEFAULT "";
DECLARE xorhash CHAR(64) DEFAULT "";
DECLARE i INT DEFAULT 1;
SET xorhash = ARC_HMAC_SHA_512(CONCAT(saltkey,UNHEX("00000001")),passwd);
SET prevhash = xorhash;
WHILE (i < l) DO
SET prevhash = ARC_HMAC_SHA_512(prevhash,passwd);
SET xorhash = ARC_XORSTRS(xorhash,prevhash);
SET i = i + 1;
END WHILE;
RETURN xorhash;
END//
DELIMITER ;