美文网首页
vite+crypto-js实现登录时MD5摘要

vite+crypto-js实现登录时MD5摘要

作者: 会爬虫的小蟒蛇 | 来源:发表于2023-04-19 07:29 被阅读0次

    crypto-js 使用

    要在Vite中引入crypto-js,您需要执行以下步骤:

    1. 首先,您需要使用npm或yarn等包管理器将crypto-js安装到您的项目中。您可以在终端中使用以下命令来安装:
    npm install crypto-js
    或者
    yarn add crypto-js
    
    1. 然后,您需要在您的JavaScript文件中引入crypto-js。您可以使用以下命令:
    import CryptoJS from 'crypto-js';
    
    1. 实现MD5加密
    // 需要加密的字符串
    const str = 'Hello, world!';
    
    // 将字符串转换为 WordArray 对象
    const wordArray = CryptoJS.enc.Utf8.parse(str);
    
    // 对 WordArray 对象进行 MD5 加密
    const md5WordArray = CryptoJS.MD5(wordArray);
    
    // 将加密后的 WordArray 对象转换为十六进制字符串
    const md5Str = CryptoJS.enc.Hex.stringify(md5WordArray);
    
    // 输出加密后的字符串
    console.log(md5Str);
    

    完整vue代码实现

    <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">智慧云党建平台</div>
    
            <div style="margin-top: 30px">
                <div id="input-box">
                    <el-icon id="input-icon">
                        <UserFilled/>
                    </el-icon>
                    <input v-model="loginFrom.username" placeholder="请输入账号"/>
                    <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="请输入密码"/>
                    <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">注册</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>
    

    相关文章

      网友评论

          本文标题:vite+crypto-js实现登录时MD5摘要

          本文链接:https://www.haomeiwen.com/subject/ttnkjdtx.html