The use of md5 in vite+vue3+ts

The use of md5 in vite+vue3+ts

Please follow my above article (vite+vue3+ts novice tutorial) to install vite+vue3+ts, etc.

md5 official website: https://www.npmjs.com/package/md5

Introduction: md5 is a hash function, mainly used to verify the integrity of data and prevent data from being tampered with. Whether it is needed according to the project api requirements

Function: Encrypt or digitally sign passwords (generate a fixed-length digest to ensure security), commonly used when docking interfaces

Often back-end interfaces require signatures to transmit information

For example

Insert image description here

A signature needs to be passed in this interface. The signature field contains order id and customer id information.

Install md5

pnpm install md5 //依次安装

pnpm install --save-dev @types/md5  //依次安装

use

Create src/utils/index.tsfile (encapsulate)

import md5 from "md5";
const KEY = 'ahkjHASdhKJsdh12k1j2jk12h3j1'//密钥 后端给的加入签名中(可能你的接口不需要)

export const stamp = () => Math.round(new Date().getTime() / 1000)//时间戳

export const autograph = (val: string = '', isKey: boolean = true, isStamp: boolean = true) => {
    
    
    const ctx = isKey ? `${
      
      KEY}${
      
      val}` : val
    const date = isStamp ? stamp() : ''
    return md5(md5(ctx) + date)
}//加密的封装

Use in projects

import {
    
     autograph, stamp } from '../../utils'

//传参时直接带入
let data = {
    
    
	stamp: stamp(),
	//Sign.modal.name和Sign.modal.phone需要加密的参数
	string: autograph(`${
      
      Sign.modal.phone}${
      
      Sign.modal.temp_code}`),
}

Guess you like

Origin blog.csdn.net/weixin_58142746/article/details/130184346