vite+crypto-js實(shí)現(xiàn)登錄時(shí)MD5摘要

crypto-js 使用

要在Vite中引入crypto-js,您需要執(zhí)行以下步驟:

  1. 首先,您需要使用npm或yarn等包管理器將crypto-js安裝到您的項(xiàng)目中。您可以在終端中使用以下命令來安裝:
npm install crypto-js
或者
yarn add crypto-js
  1. 然后,您需要在您的JavaScript文件中引入crypto-js。您可以使用以下命令:
import CryptoJS from 'crypto-js';
  1. 實(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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容