十六进制字符串(hexString)转base64

@TOC

背景

sm-crypto中的sm3加密数据时,sm3方法返回的是hexString,但是后端需要接收的是base64。使用浏览器自带的btoa(str):base64,转base64后,获取的结果是不对的(可以配合后端sm3加密测验,会发现btoa(str)转的base64和后端的不匹配)。所需需要封装一个适用hexString十六进制字符串转base64的方法。

hexString十六进制字符串转base64

/* eslint-disable no-sequences */
/* eslint-disable no-bitwise */
/* eslint-disable eqeqeq */
/* eslint-disable no-cond-assign */
/* eslint-disable no-unused-expressions */
/* eslint-disable no-array-constructor */

const base64EncodeChars =
	'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

const base64encode = function (e: any) {
	let r
	let a
	let c
	let h
	let o
	let t
	for (c = e.length, a = 0, r = ''; a < c; ) {
		if (((h = 255 & e.charCodeAt(a++)), a == c)) {
			;(r += base64EncodeChars.charAt(h >> 2)),
				(r += base64EncodeChars.charAt((3 & h) << 4)),
				(r += '==')
			break
		}
		if (((o = e.charCodeAt(a++)), a == c)) {
			;(r += base64EncodeChars.charAt(h >> 2)),
				(r += base64EncodeChars.charAt(((3 & h) << 4) | ((240 & o) >> 4))),
				(r += base64EncodeChars.charAt((15 & o) << 2)),
				(r += '=')
			break
		}
		;(t = e.charCodeAt(a++)),
			(r += base64EncodeChars.charAt(h >> 2)),
			(r += base64EncodeChars.charAt(((3 & h) << 4) | ((240 & o) >> 4))),
			(r += base64EncodeChars.charAt(((15 & o) << 2) | ((192 & t) >> 6))),
			(r += base64EncodeChars.charAt(63 & t))
	}
	return r
}

/**
 *
	16进制转base64
 */
export const hexToBase64 = function (str: any) {
	return base64encode(
		String.fromCharCode.apply(
			null,
			str
				.replace(/\r|\n/g, '')
				.replace(/([\da-fA-F]{2}) ?/g, '0x$1 ')
				.replace(/ +$/, '')
				.split(' ')
		)
	)
}

其他base64、hexString、string互转的代码

/* eslint-disable no-sequences */
/* eslint-disable no-bitwise */
/* eslint-disable eqeqeq */
/* eslint-disable no-cond-assign */
/* eslint-disable no-unused-expressions */
/* eslint-disable no-array-constructor */

const base64EncodeChars =
	'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

const base64encode = function (e: any) {
	let r
	let a
	let c
	let h
	let o
	let t
	for (c = e.length, a = 0, r = ''; a < c; ) {
		if (((h = 255 & e.charCodeAt(a++)), a == c)) {
			;(r += base64EncodeChars.charAt(h >> 2)),
				(r += base64EncodeChars.charAt((3 & h) << 4)),
				(r += '==')
			break
		}
		if (((o = e.charCodeAt(a++)), a == c)) {
			;(r += base64EncodeChars.charAt(h >> 2)),
				(r += base64EncodeChars.charAt(((3 & h) << 4) | ((240 & o) >> 4))),
				(r += base64EncodeChars.charAt((15 & o) << 2)),
				(r += '=')
			break
		}
		;(t = e.charCodeAt(a++)),
			(r += base64EncodeChars.charAt(h >> 2)),
			(r += base64EncodeChars.charAt(((3 & h) << 4) | ((240 & o) >> 4))),
			(r += base64EncodeChars.charAt(((15 & o) << 2) | ((192 & t) >> 6))),
			(r += base64EncodeChars.charAt(63 & t))
	}
	return r
}

/**
 *
	16进制转base64
 */
export const hexToBase64 = function (str: any) {
	return base64encode(
		String.fromCharCode.apply(
			null,
			str
				.replace(/\r|\n/g, '')
				.replace(/([\da-fA-F]{2}) ?/g, '0x$1 ')
				.replace(/ +$/, '')
				.split(' ')
		)
	)
}

/**
 * base64转为16进制
 */
export function base64ToHex(base64: string): string {
	const raw = atob(base64)

	let HEX = ''

	// eslint-disable-next-line no-plusplus
	for (let i = 0; i < raw.length; i++) {
		// eslint-disable-next-line no-underscore-dangle
		const _hex = raw.charCodeAt(i).toString(16)

		HEX += _hex.length === 2 ? _hex : `0${_hex}`
	}
	return HEX.toUpperCase()
}

/**
 * 随机生成16位字符串
 */
export function createNonceStr(): string {
	const chars = [
		'0',
		'1',
		'2',
		'3',
		'4',
		'5',
		'6',
		'7',
		'8',
		'9',
		'A',
		'B',
		'C',
		'D',
		'E',
		'F',
		'G',
		'H',
		'I',
		'J',
		'K',
		'L',
		'M',
		'N',
		'O',
		'P',
		'Q',
		'R',
		'S',
		'T',
		'U',
		'V',
		'W',
		'X',
		'Y',
		'Z',
		'a',
		'b',
		'c',
		'd',
		'e',
		'f',
		'g',
		'h',
		'i',
		'j',
		'k',
		'l',
		'm',
		'n',
		'o',
		'p',
		'q',
		'r',
		's',
		't',
		'u',
		'v',
		'w',
		'x',
		'y',
		'z',
	]
	let nums = ''
	// eslint-disable-next-line no-plusplus
	for (let i = 0; i < 16; i++) {
		// 这里是几位就要在这里不改变

		const id = parseInt((Math.random() * 61).toString(), 10)
		nums += chars[id]
	}
	return nums
}

/**
 * 字符串转成16进制
 */
export function stringToHex(value: string): string {
	let str = ''
	// eslint-disable-next-line no-plusplus
	for (let i = 0; i < value.length; i++) {
		if (value === '') str = value.charCodeAt(i).toString(16)
		else str += value.charCodeAt(i).toString(16)
	}
	return str
}

// 字符串转base64
export function strToBase64(preStr: string) {
	const str = encodeURI(preStr)
	const base64 = btoa(str)
	return base64
}

export function base64ToString(base64: string): string {
	return atob(base64)
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原谅我很悲

不要打赏哦,加个好友一起学习呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值