1+ /**
2+ * 分析参数加密
3+ */
4+ class ParamEncryptionAnalyzer {
5+
6+ /**
7+ *
8+ * @param param {Param}
9+ */
10+ analyze ( param ) {
11+ return this . detectEncryptionType ( param . value ) ;
12+ }
13+
14+ 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" ;
19+ }
20+
21+ // MD5
22+ const md5Regex = / ^ [ a - f 0 - 9 ] { 32 } $ / i;
23+ if ( md5Regex . test ( input ) ) {
24+ return "MD5" ;
25+ }
26+
27+ // SHA-1
28+ const sha1Regex = / ^ [ a - f 0 - 9 ] { 40 } $ / i;
29+ if ( sha1Regex . test ( input ) ) {
30+ return "SHA-1" ;
31+ }
32+
33+ // SHA-256
34+ const sha256Regex = / ^ [ a - f 0 - 9 ] { 64 } $ / i;
35+ if ( sha256Regex . test ( input ) ) {
36+ return "SHA-256" ;
37+ }
38+
39+ // SHA-512
40+ const sha512Regex = / ^ [ a - f 0 - 9 ] { 128 } $ / i;
41+ if ( sha512Regex . test ( input ) ) {
42+ return "SHA-512" ;
43+ }
44+
45+ // bcrypt
46+ const bcryptRegex = / ^ \$ 2 [ a b y ] \$ \d { 2 } \$ [ . \/ A - Z a - z 0 - 9 ] { 53 } $ / ;
47+ if ( bcryptRegex . test ( input ) ) {
48+ return "bcrypt" ;
49+ }
50+
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
78+ 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;
79+ if ( uuidRegex . test ( input ) ) {
80+ return "UUID" ;
81+ }
82+
83+ // 如果都不匹配,返回未知
84+ return null ;
85+ }
86+
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+
100+ }
101+
102+
103+ module . exports = {
104+ ParamEncryptionAnalyzer
105+ }
0 commit comments