crypto-js 使用
要在Vite中引入crypto-js,您需要執(zhí)行以下步驟:
- 首先间景,您需要使用npm或yarn等包管理器將crypto-js安裝到您的項(xiàng)目中。您可以在終端中使用以下命令來(lái)安裝:
npm install crypto-js
或者
yarn add crypto-js
- 然后,您需要在您的JavaScript文件中引入crypto-js晦譬。您可以使用以下命令:
import CryptoJS from 'crypto-js';
- 實(shí)現(xiàn)MD5加密
// 需要加密的字符串
const str = 'Hello, world!';
// 將字符串轉(zhuǎn)換為 WordArray 對(duì)象
const wordArray = CryptoJS.enc.Utf8.parse(str);
// 對(duì) WordArray 對(duì)象進(jìn)行 MD5 加密
const md5WordArray = CryptoJS.MD5(wordArray);
// 將加密后的 WordArray 對(duì)象轉(zhuǎn)換為十六進(jìn)制字符串
const md5Str = CryptoJS.enc.Hex.stringify(md5WordArray);
// 輸出加密后的字符串
console.log(md5Str);
完整vue代碼實(shí)現(xiàn)
<script setup>
import {UserFilled, Lock} from '@element-plus/icons-vue'
import {reactive} from "vue";
import CryptoJS from 'crypto-js';
import axios from "axios";
const loginFrom = reactive({username: "", password: ""})
const login = () => {
console.log(loginFrom)
console.log(md5(loginFrom.password))
// axios.post()
}
const getImg = (path) => {
return new URL(path, import.meta.url).href;
}
const md5 = (str) => {
const wordArray = CryptoJS.enc.Utf8.parse(str);
const md5WordArray = CryptoJS.MD5(wordArray);
const md5Str = CryptoJS.enc.Hex.stringify(md5WordArray);
return md5Str;
}
</script>
<template>
<div id="box">
<img id="dh" :src="getImg('../assets/image/dh.png')" alt="黨徽"/>
<div id="title">智慧云黨建平臺(tái)</div>
<div style="margin-top: 30px">
<div id="input-box">
<el-icon id="input-icon">
<UserFilled/>
</el-icon>
<input v-model="loginFrom.username" placeholder="請(qǐng)輸入賬號(hào)"/>
<el-divider id="input-divider"/>
</div>
<div id="input-box">
<el-icon id="input-icon">
<Lock/>
</el-icon>
<input v-model="loginFrom.password" type="password" placeholder="請(qǐng)輸入密碼"/>
<el-divider id="input-divider"/>
</div>
</div>
<div>
<el-button type="danger" size="large" style="width: 100px" @click="login">登錄</el-button>
<el-button type="warning" size="large" style="margin-left: 80px;width: 100px">注冊(cè)</el-button>
</div>
</div>
</template>
<style scoped>
#box {
width: 100%;
height: 100%;
background-size: 100vw 50vw;
background-repeat: no-repeat;
background-image: url("../assets/image/bg.jpg");
display: flex;
flex-direction: column;
align-items: center;
}
#dh {
margin-top: 150px;
width: 100px;
height: 100px;
}
#title {
line-height: 70px;
color: white;
font-size: 50px;
font-weight: bold;
text-shadow: 4px 2px 2px bisque;
}
input {
width: 100%;
padding: 10px 0;
font-size: 16px;
color: #fff;
margin-bottom: 30px;
border: none;
outline: none;
background-color: transparent;
}
::-webkit-input-placeholder { /* WebKit, Blink, Edge */
color: #fff;
}
#input-box {
position: relative;
width: 350px;
}
#input-icon {
position: absolute;
left: -30px;
bottom: 37px;
font-size: 30px;
color: #fff
}
#input-divider {
width: 400px;
position: absolute;
right: 0;
bottom: 5px;
left: -40px
}
</style>