|
1 | | -local CRYPT = require "lcrypt" |
| 1 | +local CRYPTO = require "lcrypt" |
2 | 2 |
|
3 | | -local hexencode = CRYPT.hexencode |
4 | | -local hexdecode = CRYPT.hexdecode |
5 | | -local base64encode = CRYPT.base64encode |
6 | | -local base64decode = CRYPT.base64decode |
| 3 | +local hexencode = CRYPTO.hexencode |
| 4 | +local hexdecode = CRYPTO.hexdecode |
| 5 | +local base64encode = CRYPTO.base64encode |
| 6 | +local base64decode = CRYPTO.base64decode |
7 | 7 |
|
8 | 8 | -- 填充方式 |
9 | | -local RSA_NO_PADDING = CRYPT.RSA_NO_PADDING |
10 | | -local RSA_PKCS1_PADDING = CRYPT.RSA_PKCS1_PADDING |
11 | | -local RSA_PKCS1_OAEP_PADDING = CRYPT.RSA_PKCS1_OAEP_PADDING |
| 9 | +local RSA_NO_PADDING = CRYPTO.RSA_NO_PADDING |
| 10 | +local RSA_PKCS1_PADDING = CRYPTO.RSA_PKCS1_PADDING |
| 11 | +local RSA_PKCS1_OAEP_PADDING = CRYPTO.RSA_PKCS1_OAEP_PADDING |
12 | 12 |
|
13 | | -local rsa_public_key_encode = CRYPT.rsa_public_key_encode |
14 | | -local rsa_private_key_decode = CRYPT.rsa_private_key_decode |
15 | | - |
16 | | -local rsa_private_key_encode = CRYPT.rsa_private_key_encode |
17 | | -local rsa_public_key_decode = CRYPT.rsa_public_key_decode |
| 13 | +local rsa_public_key_encode = CRYPTO.rsa_public_key_encode |
| 14 | +local rsa_private_key_encode = CRYPTO.rsa_private_key_encode |
| 15 | +local rsa_private_key_decode = CRYPTO.rsa_private_key_decode |
18 | 16 |
|
19 | 17 | -- 当前支持的签名与验签方法 |
20 | | -local rsa_sign = CRYPT.rsa_sign |
21 | | -local rsa_verify = CRYPT.rsa_verify |
| 18 | +local rsa_sign = CRYPTO.rsa_sign |
| 19 | +local rsa_verify = CRYPTO.rsa_verify |
22 | 20 |
|
23 | 21 | -- 当前支持的签名与验签 |
24 | 22 | local rsa_algorithms = { |
25 | | - ["md5"] = CRYPT.nid_md5, |
26 | | - ["sha1"] = CRYPT.nid_sha1, |
27 | | - ["sha128"] = CRYPT.nid_sha1, |
28 | | - ["sha256"] = CRYPT.nid_sha256, |
29 | | - ["sha512"] = CRYPT.nid_sha512, |
| 23 | + ["md5"] = CRYPTO.nid_md5, |
| 24 | + ["sha1"] = CRYPTO.nid_sha1, |
| 25 | + ["sha128"] = CRYPTO.nid_sha1, |
| 26 | + ["sha224"] = CRYPTO.nid_sha224, |
| 27 | + ["sha256"] = CRYPTO.nid_sha256, |
| 28 | + ["sha384"] = CRYPTO.nid_sha384, |
| 29 | + ["sha512"] = CRYPTO.nid_sha512, |
30 | 30 | } |
31 | 31 |
|
32 | | -local RSA = {} |
| 32 | +-- 加密后的格式 |
| 33 | +local rsa_padding = { |
| 34 | + ["oaep"] = RSA_PKCS1_OAEP_PADDING, |
| 35 | + ["pkcs1"] = RSA_PKCS1_PADDING, |
| 36 | + ["nopadding"] = RSA_NO_PADDING, |
| 37 | +} |
33 | 38 |
|
34 | | --- `text` 为原始文本内容, `public_key_path` 为公钥路径, `b64` 为是否为结果进行`base64`编码 |
35 | | -function RSA.rsa_public_key_encode(text, public_key_path, b64) |
36 | | - local hash = rsa_public_key_encode(text, public_key_path, RSA_PKCS1_PADDING) |
37 | | - if hash and b64 then |
38 | | - return base64encode(hash) |
| 39 | +local function rsa_pub_enc(text, pkey, b64, padding) |
| 40 | + local cipher = rsa_public_key_encode(text, pkey, rsa_padding[padding] or rsa_padding['pkcs1']) |
| 41 | + if cipher and b64 then |
| 42 | + return base64encode(cipher) |
39 | 43 | end |
40 | | - return hash |
| 44 | + return cipher |
41 | 45 | end |
42 | 46 |
|
43 | | --- `text` 为原始文本内容, `public_key_path` 为公钥路径, `b64` 为是否为结果进行`base64`编码 |
44 | | -function RSA.rsa_public_key_no_padding_encode(text, public_key_path, b64) |
45 | | - local hash = rsa_public_key_encode(text, public_key_path, RSA_NO_PADDING) |
46 | | - if hash and b64 then |
47 | | - return base64encode(hash) |
| 47 | +local function rsa_pri_enc(text, pkey, b64, padding, pw) |
| 48 | + local cipher = rsa_private_key_encode(text, pkey, rsa_padding[padding] or rsa_padding['pkcs1'], pw) |
| 49 | + if cipher and b64 then |
| 50 | + return base64encode(cipher) |
48 | 51 | end |
49 | | - return hash |
| 52 | + return cipher |
50 | 53 | end |
51 | 54 |
|
52 | | --- `text` 为原始文本内容, `public_key_path` 为公钥路径, `b64` 为是否为结果进行`base64`编码 |
53 | | -function RSA.rsa_public_key_oaep_padding_encode(text, public_key_path, b64) |
54 | | - local hash = rsa_public_key_encode(text, public_key_path, RSA_PKCS1_OAEP_PADDING) |
55 | | - if hash and b64 then |
56 | | - return base64encode(hash) |
| 55 | +local function rsa_pri_dec(cipher, pkey, b64, padding, pw) |
| 56 | + if b64 then |
| 57 | + cipher = base64decode(cipher) |
57 | 58 | end |
58 | | - return hash |
| 59 | + return rsa_private_key_decode(cipher, pkey, rsa_padding[padding] or rsa_padding['pkcs1'], pw) |
59 | 60 | end |
60 | 61 |
|
61 | | --- `text` 为加密后的内容, `private_key_path` 为私钥路径, `b64` 为是否为`text`先进行`base64`解码 |
62 | | -function RSA.rsa_private_key_decode(text, private_key_path, b64) |
63 | | - return rsa_private_key_decode(b64 and base64decode(text) or text, private_key_path, RSA_PKCS1_PADDING) |
64 | | -end |
| 62 | +-- local function rsa_pub_dec(cipher, pkey, b64, padding) |
| 63 | +-- if b64 then |
| 64 | +-- cipher = base64decode(cipher) |
| 65 | +-- end |
| 66 | +-- return rsa_public_key_decode(cipher, pkey, rsa_padding[padding] or rsa_padding['pkcs1']) |
| 67 | +-- end |
65 | 68 |
|
66 | | --- `text` 为加密后的内容, `private_key_path` 为私钥路径, `b64` 为是否为`text`先进行`base64`解码 |
67 | | -function RSA.rsa_private_key_no_padding_decode(text, private_key_path, b64) |
68 | | - return rsa_private_key_decode(b64 and base64decode(text) or text, private_key_path, RSA_NO_PADDING) |
| 69 | +---@class crypto |
| 70 | +local RSA = {} |
| 71 | + |
| 72 | +---------------- 私钥加密/解密 -------------------- |
| 73 | + |
| 74 | +---comment `RSA`私钥加密(`pkcs1`格式); 成功返回加密后的文本, 失败返回`false`与错误信息. |
| 75 | +---@param text string @待加密的文本 |
| 76 | +---@param prikey string @私钥内容或者私钥所在路径 |
| 77 | +---@param b64? boolean @将加密后的内容进行`BASE64`编码 |
| 78 | +---@param pw? string @如果有密码则填入. |
| 79 | +function RSA.rsa_private_key_encode(text, prikey, b64, pw) |
| 80 | + return rsa_pri_enc(text, prikey, b64 and true or false, 'pkcs1', pw) |
69 | 81 | end |
70 | 82 |
|
71 | | --- `text` 为加密后的内容, `private_key_path` 为私钥路径, `b64` 为是否为`text`先进行`base64`解码 |
72 | | -function RSA.rsa_private_key_oaep_padding_decode(text, private_key_path, b64) |
73 | | - return rsa_private_key_decode(b64 and base64decode(text) or text, private_key_path, RSA_PKCS1_OAEP_PADDING) |
| 83 | +---comment `RSA`私钥加密(`oaep`格式); 成功返回加密后的文本, 失败返回`false`与错误信息. |
| 84 | +---@param text string @待加密的文本 |
| 85 | +---@param prikey string @私钥内容或者私钥所在路径 |
| 86 | +---@param b64? boolean @将加密后的内容进行`BASE64`编码 |
| 87 | +---@param pw? string @如果有密码则填入. |
| 88 | +function RSA.rsa_private_key_oaep_padding_encode(text, prikey, b64, pw) |
| 89 | + return rsa_pri_enc(text, prikey, b64 and true or false, 'oaep', pw) |
74 | 90 | end |
75 | 91 |
|
| 92 | +---comment `RSA`私钥加密(`nopadding`); 成功返回加密后的文本, 失败返回`false`与错误信息. |
| 93 | +---@param text string @待加密的文本 |
| 94 | +---@param prikey string @私钥内容或者私钥所在路径 |
| 95 | +---@param b64 boolean @将加密后的内容进行`BASE64`编码 |
| 96 | +---@param pw? string @如果有密码则填入. |
| 97 | +function RSA.rsa_private_key_no_padding_encode(text, prikey, b64, pw) |
| 98 | + return rsa_pri_enc(text, prikey, b64 and true or false, 'nopadding', pw) |
| 99 | +end |
76 | 100 |
|
77 | | --- `text` 为原始文本内容, `private_key_path` 为公钥路径, `b64` 为是否为结果进行`base64`编码 |
78 | | -function RSA.rsa_private_key_encode(text, private_key_path, b64) |
79 | | - local hash = rsa_private_key_encode(text, private_key_path, RSA_PKCS1_PADDING) |
80 | | - if hash and b64 then |
81 | | - return base64encode(hash) |
82 | | - end |
83 | | - return hash |
| 101 | +---comment `RSA`私钥解密(`pkcs1`格式); 成功返回解密后的明文, 失败返回`false`与错误信息. |
| 102 | +---@param cipher string @已加密的文本 |
| 103 | +---@param prikey string @私钥内容或者私钥所在路径 |
| 104 | +---@param b64? boolean @内容进行`BASE64`解码 |
| 105 | +---@param pw? string @如果有密码则填入. |
| 106 | +function RSA.rsa_private_key_decode(cipher, prikey, b64, pw) |
| 107 | + return rsa_pri_dec(cipher, prikey, b64 and true or false, 'pkcs1', pw) |
84 | 108 | end |
85 | 109 |
|
86 | | --- `text` 为原始文本内容, `private_key_path` 为公钥路径, `b64` 为是否为结果进行`base64`编码 |
87 | | -function RSA.rsa_private_key_no_padding_encode(text, private_key_path, b64) |
88 | | - local hash = rsa_private_key_encode(text, private_key_path, RSA_NO_PADDING) |
89 | | - if hash and b64 then |
90 | | - return base64encode(hash) |
91 | | - end |
92 | | - return hash |
| 110 | +---comment `RSA`私钥解密(`oaep`格式); 成功返回解密后的明文, 失败返回`false`与错误信息. |
| 111 | +---@param cipher string @已加密的文本 |
| 112 | +---@param prikey string @私钥内容或者私钥所在路径 |
| 113 | +---@param b64? boolean @内容进行`BASE64`解码 |
| 114 | +---@param pw? string @如果有密码则填入. |
| 115 | +function RSA.rsa_private_key_oaep_padding_decode(cipher, prikey, b64, pw) |
| 116 | + return rsa_pri_dec(cipher, prikey, b64 and true or false, 'oaep', pw) |
93 | 117 | end |
94 | 118 |
|
95 | | --- `text` 为原始文本内容, `private_key_path` 为公钥路径, `b64` 为是否为结果进行`base64`编码 |
96 | | -function RSA.rsa_private_key_oaep_padding_encode(text, private_key_path, b64) |
97 | | - local hash = rsa_private_key_encode(text, private_key_path, RSA_PKCS1_OAEP_PADDING) |
98 | | - if hash and b64 then |
99 | | - return base64encode(hash) |
100 | | - end |
101 | | - return hash |
| 119 | +---comment `RSA`私钥解密(`nopadding`); 成功返回解密后的明文, 失败返回`false`与错误信息. |
| 120 | +---@param cipher string @已加密的文本 |
| 121 | +---@param prikey string @私钥内容或者私钥所在路径 |
| 122 | +---@param b64? boolean @内容进行`BASE64`解码 |
| 123 | +---@param pw? string @如果有密码则填入. |
| 124 | +function RSA.rsa_private_key_no_padding_decode(cipher, prikey, b64, pw) |
| 125 | + return rsa_pri_dec(cipher, prikey, b64 and true or false, 'nopadding', pw) |
102 | 126 | end |
103 | 127 |
|
104 | | --- -- `text` 为加密后的内容, `public_key_path` 为公钥路径, `b64`为是否为`text·先进行`base64`解码 |
105 | | --- function RSA.rsa_public_key_decode(text, public_key_path, b64) |
106 | | --- return rsa_public_key_decode(b64 and base64decode(text) or text, public_key_path, RSA_PKCS1_PADDING) |
107 | | --- end |
| 128 | +---------------- 公钥加密/解密 -------------------- |
108 | 129 |
|
109 | | --- function RSA.rsa_public_key_no_padding_decode(text, public_key_path, b64) |
110 | | --- return rsa_public_key_decode(b64 and base64decode(text) or text, public_key_path, RSA_NO_PADDING) |
111 | | --- end |
| 130 | +---comment `RSA`公钥加密(`pkcs1`格式); 成功返回加密后的文本, 失败返回`false`与错误信息. |
| 131 | +---@param text string @待加密的文本 |
| 132 | +---@param pubkey string @公钥内容或者公钥所在路径 |
| 133 | +---@param b64? boolean @将加密后的内容进行`BASE64`编码 |
| 134 | +function RSA.rsa_public_key_encode(text, pubkey, b64) |
| 135 | + return rsa_pub_enc(text, pubkey, b64 and true or false, 'pkcs1') |
| 136 | +end |
112 | 137 |
|
113 | | --- function RSA.rsa_public_key_oaep_padding_decode(text, public_key_path, b64) |
114 | | --- return rsa_public_key_decode(b64 and base64decode(text) or text, public_key_path, RSA_PKCS1_OAEP_PADDING) |
115 | | --- end |
| 138 | +---comment `RSA`公钥加密(`oaep`格式); 成功返回加密后的文本, 失败返回`false`与错误信息. |
| 139 | +---@param text string @待加密的文本 |
| 140 | +---@param pubkey string @公钥内容或者公钥所在路径 |
| 141 | +---@param b64? boolean @将加密后的内容进行`BASE64`编码 |
| 142 | +function RSA.rsa_public_key_oaep_padding_encode(text, pubkey, b64) |
| 143 | + return rsa_pub_enc(text, pubkey, b64 and true or false, 'oaep') |
| 144 | +end |
| 145 | + |
| 146 | +---comment `RSA`公钥加密(`nopadding`); 成功返回加密后的文本, 失败返回`false`与错误信息. |
| 147 | +---@param text string @待加密的文本 |
| 148 | +---@param pubkey string @公钥内容或者公钥所在路径 |
| 149 | +---@param b64? boolean @将加密后的内容进行`BASE64`编码 |
| 150 | +function RSA.rsa_public_key_no_padding_encode(text, pubkey, b64) |
| 151 | + return rsa_pub_enc(text, pubkey, b64 and true or false, 'nopadding') |
| 152 | +end |
116 | 153 |
|
117 | | --- RSA签名函数: 第一个参数是等待签名的明文, 第二个参数是私钥所在路径, 第三个参数是算法名称, 第四个参数决定是否以hex输出 |
118 | | -function RSA.rsa_sign(text, private_key_path, algorithm, hex) |
119 | | - local hash = rsa_sign(text, private_key_path, rsa_algorithms[(algorithm or ""):lower()] or rsa_algorithms["md5"]) |
120 | | - if hash and hex then |
121 | | - return hexencode(hash) |
| 154 | +---------------------------------------------------------------------------------------------------- |
| 155 | + |
| 156 | +---comment `RSA`签名函数(目前支持:`md5`、`sha128` ~ `sha512`) |
| 157 | +---@param text string @待签名的明文 |
| 158 | +---@param prikey string @私钥内容或者所在路径 |
| 159 | +---@param alg "md5"|"sha128"|"sha224"|"sha256"|"sha384"|"sha512" @签名算法(例如: `"md5"`) |
| 160 | +---@param hex? 'base64' | boolean @签名是否编码(可选) |
| 161 | +function RSA.rsa_sign(text, prikey, alg, hex) |
| 162 | + local sign = rsa_sign(text, prikey, rsa_algorithms[alg] or rsa_algorithms['md5']) |
| 163 | + if sign and hex then |
| 164 | + if hex == 'base64' then |
| 165 | + sign = base64encode(sign) |
| 166 | + else |
| 167 | + sign = hexencode(sign) |
| 168 | + end |
122 | 169 | end |
123 | | - return hash |
| 170 | + return sign |
124 | 171 | end |
125 | 172 |
|
126 | | --- RSA验签函数: 第一个参数是等待签名的明文, 第二个参数是私钥所在路径, 第三个参数为签名sign密文, 第四个参数是算法名称, 第五个参数决定是否对sign进行unhex |
127 | | -function RSA.rsa_verify(text, public_key_path, sign, algorithm, hex) |
| 173 | +---comment `RSA`验签函数(目前支持:`md5`、`sha128` ~ `sha512`) |
| 174 | +---@param text string @待签名的明文 |
| 175 | +---@param pubkey string @公钥内容或者所在路径 |
| 176 | +---@param sign string @待对比的签名内容 |
| 177 | +---@param alg "md5"|"sha128"|"sha224"|"sha256"|"sha384"|"sha512" @签名算法(例如: `"md5"`) |
| 178 | +---@param hex? 'base64' | boolean @`sign`是否解码(可选) |
| 179 | +function RSA.rsa_verify(text, pubkey, sign, alg, hex) |
128 | 180 | if hex then |
129 | | - sign = hexdecode(sign) |
| 181 | + if hex == 'base64' then |
| 182 | + sign = base64decode(sign) |
| 183 | + else |
| 184 | + sign = hexdecode(sign) |
| 185 | + end |
130 | 186 | end |
131 | | - return rsa_verify(text, public_key_path, sign, rsa_algorithms[(algorithm or ""):lower()] or rsa_algorithms["md5"]) |
| 187 | + return rsa_verify(text, pubkey, sign, rsa_algorithms[alg] or rsa_algorithms['md5']) |
132 | 188 | end |
133 | 189 |
|
134 | 190 | -- 初始化函数 |
|
0 commit comments