@joingo/sdk-security
Version:
SDK for JavaScript: Security Application Block!
47 lines (43 loc) • 2.63 kB
text/typescript
/* ************************************************************************************************************************ *\
* SDK for JavaScript! *
* *
* COPYRIGHT © 2021 BEIJING JOINGO INFORMATION TECHNOLOGY CO., LTD. ALL RIGHTS RESERVED. *
* LICENSED UNDER THE MIT LICENSE. SEE LICENSE FILE IN THE PROJECT ROOT FOR FULL LICENSE INFORMATION. *
* *
* AUTHOR : WANG YUCAI *
* E-MAIL ADDRESS: WANGYUCAI@JOINGO.VIP *
* DATE TIME : 2021-12-29 13:16 *
\* ************************************************************************************************************************ */
// CODEFILE: base-64.ts
// FEATURE: 提供了 BASE64 编码相关的方法。
// FILE-VERSION: v2021.12.29-build.1316
import { isNullOrEmpty } from "@joingo/sdk-core";
import CryptoJS from "crypto-js";
/**
* 将字符串 s 转换成 BASE64 字符串。
*
* @export
* @param {string} s 字符串。
* @returns {string}
*/
export function toBase64String(s: string): string {
if (isNullOrEmpty(s)) return "";
console.debug(`[DEBUG]: 尝试将字符串 "${s}" 转换成 BASE64 字符串。`);
const base64Str = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(s));
console.debug(`[DEBUG]: 字符串 "${s}" 的 BASE64 编码字符串为 "${base64Str}"。详情参见:%o`, { raw: s, "base64-str": base64Str });
return base64Str;
}
/**
* 将 BASE64 字符串 base64Str 还原成原始字符串。
*
* @export
* @param {string} base64Str 需要解码的 BASE64 字符串。
* @returns {string}
*/
export function fromBase64String(base64Str: string): string {
if (isNullOrEmpty(base64Str)) return "";
console.debug(`[DEBUG]: 尝试将 BASE64 字符串 "${base64Str}" 解码。`);
const str = CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(base64Str));
console.debug(`[DEBUG]: BASE64 字符串 "${base64Str}" 解码字符串为 "${str}"。详情参见:%o`, { raw: str, "base64-str": base64Str });
return str;
}