11/**
2- * 分析参数加密
2+ * 参数加密分析器类,用于检测输入参数的加密类型。
33 */
44class ParamEncryptionAnalyzer {
55
66 /**
7- *
8- * @param param {Param}
7+ * 分析参数的加密类型。
8+ * @param {Param } param - 需要分析的参数对象,包含一个 `value` 属性。
9+ * @returns {string|null } 返回检测到的加密类型,如果无法识别则返回 `null`。
910 */
1011 analyze ( param ) {
1112 return this . detectEncryptionType ( param . value ) ;
1213 }
1314
15+ /**
16+ * 检测输入字符串的加密类型。
17+ * @param {string } input - 需要检测的输入字符串。
18+ * @returns {string|null } 返回检测到的加密类型,如果无法识别则返回 `null`。
19+ */
1420 detectEncryptionType ( input ) {
15- // Base64
16- const base64Regex = / ^ [ A - Z a - z 0 - 9 + / ] + = { 0 , 2 } $ / ;
17- if ( base64Regex . test ( input ) && input . length % 4 === 0 ) {
18- return "Base64" ;
21+
22+ // 如果输入为空,直接返回 null
23+ if ( ! input ) {
24+ return null ;
1925 }
2026
21- // MD5
27+ // // Base64 编码检测
28+ // const base64Regex = /^[A-Za-z0-9+/]+={0,2}$/;
29+ // if (base64Regex.test(input) && input.length % 4 === 0) {
30+ // return "Base64";
31+ // }
32+
33+ // MD5 哈希检测
2234 const md5Regex = / ^ [ a - f 0 - 9 ] { 32 } $ / i;
2335 if ( md5Regex . test ( input ) ) {
2436 return "MD5" ;
2537 }
2638
27- // SHA-1
39+ // SHA-1 哈希检测
2840 const sha1Regex = / ^ [ a - f 0 - 9 ] { 40 } $ / i;
2941 if ( sha1Regex . test ( input ) ) {
3042 return "SHA-1" ;
3143 }
3244
33- // SHA-256
45+ // SHA-256 哈希检测
3446 const sha256Regex = / ^ [ a - f 0 - 9 ] { 64 } $ / i;
3547 if ( sha256Regex . test ( input ) ) {
3648 return "SHA-256" ;
3749 }
3850
39- // SHA-512
51+ // SHA-512 哈希检测
4052 const sha512Regex = / ^ [ a - f 0 - 9 ] { 128 } $ / i;
4153 if ( sha512Regex . test ( input ) ) {
4254 return "SHA-512" ;
4355 }
4456
45- // bcrypt
57+ // bcrypt 哈希检测
4658 const bcryptRegex = / ^ \$ 2 [ a b y ] \$ \d { 2 } \$ [ . \/ A - Z a - z 0 - 9 ] { 53 } $ / ;
4759 if ( bcryptRegex . test ( input ) ) {
4860 return "bcrypt" ;
4961 }
5062
51- // URL编码
52- const urlEncodedRegex = / % [ 0 - 9 A - F a - f ] { 2 } / ;
53- if ( urlEncodedRegex . test ( input ) ) {
54- return "URL Encoded" ;
55- }
56-
57- // Hex编码
58- const hexRegex = / ^ [ 0 - 9 A - F a - f ] + $ / ;
59- if ( hexRegex . test ( input ) && input . length % 2 === 0 ) {
60- return "Hex Encoded" ;
61- }
62-
63- // ROT13
64- const rot13Regex = / ^ [ A - Z a - z ] + $ / ;
65- if ( rot13Regex . test ( input ) && input === input . replace ( / [ A - Z a - z ] / g, function ( c ) {
66- return String . fromCharCode ( c . charCodeAt ( 0 ) + ( c . toLowerCase ( ) < 'n' ? 13 : - 13 ) ) ;
67- } ) ) {
68- return "ROT13" ;
69- }
70-
71- // JWT
72- const jwtRegex = / ^ [ A - Z a - z 0 - 9 - _ ] + \. [ A - Z a - z 0 - 9 - _ ] + \. [ A - Z a - z 0 - 9 - _ ] * $ / ;
73- if ( jwtRegex . test ( input ) ) {
74- return "JWT" ;
75- }
76-
77- // UUID
63+ // // URL 编码检测
64+ // const urlEncodedRegex = /%[0-9A-Fa-f]{2}/;
65+ // if (urlEncodedRegex.test(input)) {
66+ // return "URL Encoded";
67+ // }
68+ //
69+ // // Hex 编码检测
70+ // const hexRegex = /^[0-9A-Fa-f]+$/;
71+ // if (hexRegex.test(input) && input.length % 2 === 0) {
72+ // return "Hex Encoded";
73+ // }
74+
75+ // // ROT13 编码检测
76+ // const rot13Regex = /^[A-Za-z]+$/;
77+ // if (rot13Regex.test(input) && input === input.replace(/[A-Za-z]/g, function (c) {
78+ // return String.fromCharCode(c.charCodeAt(0) + (c.toLowerCase() < 'n' ? 13 : -13));
79+ // })) {
80+ // return "ROT13";
81+ // }
82+
83+ // // JWT (JSON Web Token) 检测
84+ // const jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
85+ // if (jwtRegex.test(input)) {
86+ // return "JWT";
87+ // }
88+
89+ // UUID 检测
7890 const uuidRegex = / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 12 } $ / i;
7991 if ( uuidRegex . test ( input ) ) {
8092 return "UUID" ;
8193 }
8294
83- // 如果都不匹配,返回未知
95+ // 如果以上所有加密类型都不匹配,返回 null 表示未知加密类型
8496 return null ;
8597 }
8698
87- // // 测试示例
88- // console.log(detectEncryptionType("SGVsbG8gV29ybGQ=")); // Base64
89- // console.log(detectEncryptionType("5d41402abc4b2a76b9719d911017c592")); // MD5
90- // console.log(detectEncryptionType("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12")); // SHA-1
91- // console.log(detectEncryptionType("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); // SHA-256
92- // console.log(detectEncryptionType("$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy")); // bcrypt
93- // console.log(detectEncryptionType("Hello%20World")); // URL Encoded
94- // console.log(detectEncryptionType("48656c6c6f20576f726c64")); // Hex Encoded
95- // console.log(detectEncryptionType("Uryyb Jbeyq")); // ROT13
96- // console.log(detectEncryptionType("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")); // JWT
97- // console.log(detectEncryptionType("550e8400-e29b-41d4-a716-446655440000")); // UUID
98- // console.log(detectEncryptionType("randomstring")); // Unknown Encryption Type
99-
10099}
101100
102-
101+ // 导出 ParamEncryptionAnalyzer 类
103102module . exports = {
104103 ParamEncryptionAnalyzer
105104}
0 commit comments