@@ -17,15 +17,15 @@ export function fillKey(key: string): Buffer | string {
1717 return filledKey ;
1818}
1919
20- function aesEncrypt ( text , originKey ) {
20+ function aesEncrypt ( text : string , originKey : string ) {
2121 const key = CryptoJS . enc . Utf8 . parse ( fillKey ( originKey ) ) ;
2222 return CryptoJS . AES . encrypt ( text , key , {
2323 mode : CryptoJS . mode . ECB ,
2424 padding : CryptoJS . pad . ZeroPadding
2525 } ) . toString ( ) ;
2626}
2727
28- function rsaEncrypt ( text , pubKey ) {
28+ function rsaEncrypt ( text : string , pubKey : string ) {
2929 if ( ! text ) {
3030 return text ;
3131 }
@@ -34,13 +34,13 @@ function rsaEncrypt(text, pubKey) {
3434 return jsEncrypt . encrypt ( text ) ;
3535}
3636
37- function rsaDecrypt ( cipher , pkey ) {
37+ function rsaDecrypt ( cipher : string , pkey : string ) {
3838 const jsEncrypt = new JSEncrypt ( ) ;
3939 jsEncrypt . setPrivateKey ( pkey ) ;
4040 return jsEncrypt . decrypt ( cipher ) ;
4141}
4242
43- function hexToBytes ( hex ) {
43+ function hexToBytes ( hex : string ) {
4444 if ( ! hex ) return new Uint8Array ( [ ] ) ;
4545 hex = hex . toString ( ) . trim ( ) . toLowerCase ( ) ;
4646 if ( hex . startsWith ( '0x' ) ) {
@@ -55,7 +55,7 @@ function hexToBytes(hex) {
5555 return bytes ;
5656}
5757
58- function bytesToBase64 ( bytes ) {
58+ function bytesToBase64 ( bytes : Uint8Array ) {
5959 // Uint8Array -> base64(标准 base64)
6060 let binary = '' ;
6161 for ( let i = 0 ; i < bytes . length ; i ++ ) {
@@ -64,15 +64,15 @@ function bytesToBase64(bytes) {
6464 return btoa ( binary ) ;
6565}
6666
67- function rsaEncryptPassword ( password , rsaPublicKey ) {
67+ function rsaEncryptPassword ( password : string , rsaPublicKey : string ) {
6868 const aesKey = ( Math . random ( ) + 1 ) . toString ( 36 ) . substring ( 2 ) ;
6969 // public key 是 base64 存储的
7070 const keyCipher = rsaEncrypt ( aesKey , rsaPublicKey ) ;
7171 const passwordCipher = aesEncrypt ( password , aesKey ) ;
7272 return `${ keyCipher } :${ passwordCipher } ` ;
7373}
7474
75- function ensureSm2PublicKey ( sm2PublicKey ) {
75+ function ensureSm2PublicKey ( sm2PublicKey : string ) {
7676 // sm2.min.js 的 doEncrypt 需要能被 decodePointHex 解析的公钥:
7777 // 通常为非压缩点 hex,格式 `04||x||y`(总长度 130)。
7878 // 但后端生成/下发的公钥有时是 `x||y`(长度 128),这里做归一化补齐 `04` 前缀。
@@ -89,7 +89,7 @@ function ensureSm2PublicKey(sm2PublicKey) {
8989 return sm2PublicKey ;
9090}
9191
92- function gmEncryptPassword ( password , sm2PublicKey ) {
92+ function gmEncryptPassword ( password : string , sm2PublicKey : string ) {
9393 sm2PublicKey = ensureSm2PublicKey ( sm2PublicKey ) ;
9494 // 只适配前端,不改后端:
9595 // 直接生成 16 字符 key(后端 padding_key 会保持原样,不再补齐)
@@ -116,7 +116,7 @@ function gmEncryptPassword(password, sm2PublicKey) {
116116 return `${ keyCipherB64 } :${ passwordCipherB64 } ` ;
117117}
118118
119- export function encryptPassword ( password ) {
119+ export function encryptPassword ( password : string ) {
120120 if ( ! password ) {
121121 console . log ( 'password is empty' ) ;
122122 return '' ;
@@ -142,7 +142,7 @@ export function encryptPassword(password) {
142142 return cipher ;
143143}
144144
145- export function randomString ( length ) {
145+ export function randomString ( length : number ) {
146146 const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
147147 let result = '' ;
148148 const charactersLength = characters . length ;
0 commit comments