
回复
本文旨在深入探讨华为鸿蒙HarmonyOS Next系统(截止目前 API12)在开发多语言电商平台方面的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。
在当今数字化时代,密码类数据的保护对于应用的安全性和用户体验至关重要。无论是登录账号、进行金融交易还是访问敏感信息,密码都起着关键的作用。HarmonyOS Next作为一款先进的操作系统,其提供的Asset Store Kit为密码类数据的安全存储和管理提供了强大的解决方案。
import { asset } from '@kit.AssetStoreKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
function stringToArray(str: string): Uint8Array {
let textEncoder = new util.TextEncoder();
return textEncoder.encodeInto(str);
}
// 假设密码为"myPassword",别名设置为"loginPassword"
let attr: asset.AssetMap = new Map();
attr.set(asset.Tag.SECRET, stringToArray('myPassword'));
attr.set(asset.Tag.ALIAS, stringToArray('loginPassword'));
try {
asset.add(attr).then(() => {
console.info('Password stored successfully.');
}).catch((err: BusinessError) => {
console.error('Failed to store password. Code is ${err.code}, message is ${err.message}');
});
} catch (error) {
let err = error as BusinessError;
console.error('Failed to store password. Code is ${err.code}, message is ${err.message}');
}
import { asset } from '@kit.AssetStoreKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
function stringToArray(str: string): Uint8Array {
let textEncoder = new util.TextEncoder();
return textEncoder.encodeInto(str);
}
function arrayToString(arr: Uint8Array): string {
let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true });
let str = textDecoder.decodeWithStream(arr, { stream: false });
return str;
}
// 存储密码
async function storePassword(password: string, alias: string) {
let attr: asset.AssetMap = new Map();
attr.set(asset.Tag.SECRET, stringToArray(password));
attr.set(asset.Tag.ALIAS, stringToArray(alias));
try {
await asset.add(attr);
console.info('Password stored successfully.');
} catch (error) {
let err = error as BusinessError;
console.error('Failed to store password. Code is ${err.code}, message is ${err.message}');
}
}
// 查询密码
async function queryPassword(alias: string) {
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray(alias));
try {
let res: Array<asset.AssetMap> = await asset.query(query);
if (res.length > 0) {
let secret: Uint8Array = res[0].get(asset.Tag.SECRET) as Uint8Array;
let password = arrayToString(secret);
console.info('Password retrieved successfully: ', password);
} else {
console.info('Password not found.');
}
} catch (error) {
let err = error as BusinessError;
console.error('Failed to query password. Code is ${err.code}, message is ${err.message}');
}
}
// 删除密码
async function deletePassword(alias: string) {
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray(alias));
try {
await asset.remove(query);
console.info('Password deleted successfully.');
} catch (error) {
let err = error as BusinessError;
console.error('Failed to delete password. Code is ${err.code}, message is ${err.message}');
}
}
// 示例用法
storePassword('myNewPassword', 'newLoginPassword');
queryPassword('newLoginPassword');
deletePassword('newLoginPassword');