Last active
November 14, 2023 06:18
-
-
Save chhumsina/71365dce96e74ac4c53f97facdf70593 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// vuejs with crypto-js - AES256 encrypt | decrypt api login | |
import CryptoJS from 'crypto-js' | |
import axios from 'axios' | |
import format from 'date-fns/format' | |
const key = '82f2ceed4c503896c8a291e560bd4325' // change to your key | |
const iv = 'sinasinasisinaaa' // change to your iv | |
const apiKey = '123xxxyyyzzz' // change to your api key | |
const username = 'chhumsina' | |
const password = 'chhumsina@123' | |
const reqTime = format(new Date(), 'yyyyMMddHHmmss') | |
export default { | |
data() { | |
return { | |
token: null | |
} | |
}, | |
created() { | |
this.login() | |
}, | |
methods: { | |
aesEncrypt(txt) { | |
const cipher = CryptoJS.AES.encrypt(txt, CryptoJS.enc.Utf8.parse(key), { | |
iv: CryptoJS.enc.Utf8.parse(iv), | |
mode: CryptoJS.mode.CBC | |
}) | |
return cipher.toString() | |
}, | |
aesDencrypt(txt) { | |
const cipher = CryptoJS.AES.decrypt(txt, CryptoJS.enc.Utf8.parse(key), { | |
iv: CryptoJS.enc.Utf8.parse(iv), | |
mode: CryptoJS.mode.CBC | |
}) | |
return CryptoJS.enc.Utf8.stringify(cipher).toString() | |
}, | |
async login() { | |
const encPassword = this.aesEncrypt(password) | |
const hash = apiKey + reqTime + username + encPassword | |
const encHash = this.aesEncrypt(hash) | |
const data = { | |
req_time: reqTime, | |
username, | |
password: encPassword, | |
hash: encHash | |
} | |
const headers = { | |
'api-key': apiKey | |
} | |
await axios | |
.post('https://your-api-url.com/login', data, { | |
headers | |
}) | |
.then((res) => { | |
this.token = res.data.token | |
}) | |
.catch((error) => { | |
console.log(error) | |
}) | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment