全局说明
编写说明
- uts在安卓端编译为
kotlin,所以,使用可以使用安卓自带库+kotlin的方法来实现 - uts在鸿蒙端编译为
ArkTs,ArkTs和UTS很相似,包括一些方法都一样,所以可以直接从ArkTs的文档里复制代码,稍微修改即可使用。
使用的库
- 安卓端
typescript
import MessageDigest from 'java.security.MessageDigest';
import BigInteger from 'java.math.BigInteger';
import Base64 from 'java.util.Base64';
- 鸿蒙端
typescript
import util from '@ohos.util';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
插件接口定义
typescript
/**
* interface.uts
* uts插件接口定义文件,按规范定义接口文件可以在HBuilderX中更好的做到语法提示
*/
/**
* 哈希算法枚举
*/
export type HashAlgorithm =
| "MD5"
| "SHA1"
| "SHA224"
| "SHA256"
| "SHA384"
| "SHA512"
/**
* 哈希加密返回结果
*/
export type HashResult = {
hash: string
}
/**
* 哈希加密函数定义
*/
export type HashFunction = (input: string, algorithm: HashAlgorithm) => string
1. Base64编解码
安卓端
typescript
/**
* BASE64编码方法
*
* @param input 输入字符串
* @return BASE64加密后的字符串
*/
export const Base64Encode = function (input: string) : string {
try {
// 将字符串转换为字节数组
// toByteArray()为kotlin的方法
const inputBytes = input.toByteArray();
// 使用Base64编码器进行编码
const encodedBytes = Base64.getEncoder().encodeToString(inputBytes);
// 将编码后的字节数组转换为字符串
return encodedBytes;
} catch (e) {
console.error("BASE64加密错误:", e);
return "";
}
}
/**
* BASE64解码方法
*
* @param input 输入字符串
* @return BASE64解密后的字符串
*/
export const Base64Decode = function (input: string) : string {
try {
// 将Base64字符串转换为字节数组
const decodedBytes = Base64.getDecoder().decode(input);
// 将Java字节数组转换为UTS字符串
return new String(decodedBytes);
} catch (e) {
console.error("BASE64解密错误:", e);
return "";
}
}
鸿蒙端
typescript
/**
* BASE64编码方法
*
* @param input 输入字符串
* @return BASE64加密后的字符串
*/
export const Base64Encode = function (input: string) : string {
let textEncoder = new util.TextEncoder("utf-8");
let uint8Array = textEncoder.encodeInto(input);
let base64Helper = new util.Base64Helper();
return base64Helper.encodeToStringSync(uint8Array);
}
/**
* BASE64解码方法
*
* @param input 输入字符串
* @return BASE64解密后的字符串
*/
export const Base64Decode = function (input: string) : string {
let Base64Helper = new util.Base64Helper();
let arr = Base64Helper.decodeSync(input)
let textDecoder = util.TextDecoder.create('utf-8');
return textDecoder.decodeToString(arr);
}
消息摘要计算
HASH加密使用统一方法,包含MD5、SHA1、SHA224、SHA256、SHA384、SHA512
安卓端
typescript
/**
* 统一哈希加密方法
*
* @param input 输入字符串
* @param algorithm 哈希算法枚举
* @return 哈希加密后的十六进制字符串
*/
export const hash : HashFunction = function (input : string, algorithm: HashAlgorithm) : string {
try {
// 创建MessageDigest实例
const md = MessageDigest.getInstance(algorithm);
// 输入数据转化为字节数组
const dataArray = input.toByteArray()
// 计算哈希值
const hashBytes = md.digest(dataArray);
// 转换为十六进制字符串
const result = BigInteger(1, hashBytes).toString(16)
return result;
} catch (e) {
// 方法出错时返回空字符串
console.error(`${algorithm}加密错误:`, e);
return "";
}
}
鸿蒙端
typescript
/**
* 统一哈希加密方法
*
* @param input 输入字符串
* @param algorithm 哈希算法枚举
* @return 哈希加密后的十六进制字符串
*/
export const hash : HashFunction = function (input : string, algorithm: HashAlgorithm) : string {
try {
// 创建哈希实例
let md = cryptoFramework.createMd(algorithm);
// 使用同步方法更新数据
let textEncoder = util.TextEncoder.create('utf-8');
let dataBlob : cryptoFramework.DataBlob = {
data: textEncoder.encodeInto(input);
};
md.updateSync(dataBlob);
// 使用同步方法计算摘要
let mdResult : cryptoFramework.DataBlob = md.digestSync();
// 转换为十六进制字符串
let result = Array.from(mdResult.data).map(byte => byte.toString(16).padStart(2, '0')).join('');
return result;
} catch (e) {
// 方法出错时返回空字符串
// console.error(`${algorithm}加密错误:`, e);
return "";
}
}
规范调用方法
这里安卓端和鸿蒙端相同
typescript
// MD5加密
export const MD5 = function (input: string) : string {
return hash(input, 'MD5')
}
// SHA1加密
export const SHA1 = function (input: string) : string {
return hash(input, 'SHA1')
}
// SHA224加密
export const SHA224 = function (input: string) : string {
return hash(input, 'SHA224')
}
// SHA256加密
export const SHA256 = function (input: string) : string {
return hash(input, 'SHA256')
}
// SHA384加密
export const SHA384 = function (input: string) : string {
return hash(input, 'SHA384')
}
// SHA512加密
export const SHA512 = function (input: string) : string {
return hash(input, 'SHA512')
}
使用方法
typescript
import * as KhCrypto from '@/uni_modules/kh-crypto'
const input = ref<string>('待加密字符串');
const output = ref<string>('')
// base64编码
output.value = KhCrypto.Base64Encode(inputText.value)
// base64解码
output.value = KhCrypto.Base64Decode(inputText.value)
// MD5加密
output.value = KhCrypto.MD5(inputText.value)
// SHA1加密
output.value = KhCrypto.SHA1(inputText.value)
// SHA224加密
output.value = KhCrypto.SHA224(inputText.value)
// SHA256加密
output.value = KhCrypto.SHA256(inputText.value)
// SHA384加密
output.value = KhCrypto.SHA384(inputText.value)
// SHA512加密
output.value = KhCrypto.SHA512(inputText.value)