依赖 jsencrypt encryptlong jsrsasign npm库
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| import JSEncrypt from 'jsencrypt' import Encrypt from 'encryptlong' import jsrsasign from 'jsrsasign'
export const generateRandomString = (length) => { let result = ''; let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let charactersLength = characters.length; for (let i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; }
export const generateRSASign = (params, privateKey)=>{ const sortedParams = Object.keys(params).sort(); const paramString = sortedParams.map(key => `${params[key]}`).join('/n'); const rsa = new jsrsasign.RSAKey() rsa = jsrsasign.KEYUTIL.getKey(privateKey) const signature = new jsrsasign.KJUR.crypto.Signature({alg: 'SHA256withRSA'}) signature.init(rsa) signature.updateString(paramString) const signResult = signature.sign() const signBase64 = jsrsasign.hextob64(signResult) return signBase64 }
export const verifyRSASign = (signData, data, publicKey) =>{ try { const rsa = new jsrsasign.RSAKey() rsa = jsrsasign.KEYUTIL.getKey(publicKey) let signatureVf = new jsrsasign.KJUR.crypto.Signature({ alg: "SHA256withRSA", prvkeypem: rsa }) signatureVf.updateString(data) let result = signatureVf.verify(jsrsasign.b64tohex(signData)) console.log("jsrsasign verify: " + result) return result } catch (e) { console.error(e) } }
export const getJSEncrypt = (data,publicKey)=>{ let jsencrypt = new JSEncrypt() jsencrypt.setPublicKey(publicKey) let result = jsencrypt.encrypt(data) return result }
export const getJSDecrypt = (data,privateKey)=>{ let jsencrypt = new JSEncrypt() jsencrypt.setPrivateKey(privateKey) let result = jsencrypt.encrypt(data) return result }
export const getEncrypt = (data,publicKey)=>{ let encrypt = new Encrypt(); encrypt.setPublicKey(publicKey); let result = encrypt.encryptLong(JSON.stringify(data)) return result }
export const getDecrypt = (data,privateKey)=>{ let decrypt = new Encrypt(); decrypt.setPrivateKey(privateKey); let result = decrypt.decryptLong(data) return result }
|