因安全需要,应用的关键信息需要在端侧进行加密,在云侧进行解密,以判断数据的合法性,端侧使用的是HMACSHA1算法,使用ArkTS实现得到的加密结果跟云侧Java侧加密结果不一致,导致云侧校验不通过。
let macAlgName = 'SHA1';
let mac = cryptoFramework.createMac(macAlgName);
let arr = stringToUint8Array('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
let KeyBLob: cryptoFramework.DataBlob = { data: arr };
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('HMAC');
const symKey = await symKeyGenerator.convertKey(KeyBlob);
await mac.init(symKey)
await mac.update({ data: stringToUint8Array(message) });
let macOutot = await mac.doFinal(); public String generateResponseCode(byte[] challenge)
throws GeneralSecurityException, Base32String.DecodingException {
String secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
byte[] keyBytes = secret.getBytes();
final Mac mac = Mac.getInstance("HMACSHA1");
mac.init(new SecretKeySpec(keyBytes, ""));
byte[] hash = mac.doFinal(challenge); ArkTS中Int8Array类型数组表示一个二进制位有符号整数数组。它的取值范围是-128到127。需要将doFinal返回的数组转换为Int8Array的类型以实现与Java侧保持的一致。