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
| const CryptoJS = require('crypto-js') const jsrsasign = require('jsrsasign') const axios = require('axios')
const createToken = (AESKey, iv, timestamp, publicKey) => { const keyString = CryptoJS.enc.Base64.stringify(AESKey) const ivString = CryptoJS.enc.Base64.stringify(iv) const text = `${keyString},${ivString},${timestamp}` const encrypted = jsrsasign.KJUR.crypto.Cipher.encrypt(text, publicKey) return jsrsasign.hextob64(encrypted) }
const encryptPassword = (password, AESKey, iv) => { const encrypted = CryptoJS.AES.encrypt(password, AESKey, {iv}) return encrypted.toString() }
const Axios = axios.create({ baseURL: 'http://127.0.0.1:7001', });
const login = async () => { const email = 'changjunhao@meimiao.net' const password = 'Life.like.summer.flowers'
const { secretKey }= ( await Axios.get('/secretKey')).data
const publicKey = jsrsasign.KEYUTIL.getKey(jsrsasign.b64toutf8(secretKey))
const salt = CryptoJS.lib.WordArray.random(128/8)
const AESKey = CryptoJS.PBKDF2('Life like summer flowers', salt, {keySize: 256/32})
const iv = CryptoJS.lib.WordArray.random(16)
const timestamp = new Date().getTime()
const token = createToken(AESKey, iv, timestamp, publicKey)
const passwordEncrypted = encryptPassword(password, AESKey, iv) return(await Axios.request({ url: '/login', method: 'POST', headers: { timestamp, token, }, data: { email, password: passwordEncrypted } })).data }
|