1/* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import huks from '@ohos.security.huks'; 16import { HuksSignVerifyDSA } from "./signverify/publicSignverifyParam"; 17import Data from "../data.json"; 18let srcData65 = Data.Data65b; 19 20function stringToArray(str) { 21 let arr = []; 22 for (let i = 0, j = str.length; i < j; ++i) { 23 arr.push(str.charCodeAt(i)); 24 } 25 return arr; 26} 27 28function uint8ArrayToString(fileData) { 29 let dataString = ''; 30 for (let i = 0; i < fileData.length; i++) { 31 dataString += String.fromCharCode(fileData[i]); 32 } 33 return dataString; 34} 35function stringToUint8Array(str) { 36 let arr = []; 37 for (let i = 0, j = str.length; i < j; ++i) { 38 arr.push(str.charCodeAt(i)); 39 } 40 let tmpUint8Array = new Uint8Array(arr); 41 return tmpUint8Array; 42} 43function arrayEqual(a, b) { 44 if ((!a instanceof Array) || (!b instanceof Array)) { 45 return false; 46 } 47 if (a.length !== b.length) { 48 return false; 49 } 50 for (let i = 0; i < a.length; ++i) { 51 if ((!a instanceof Number) || (!b instanceof Number)) { 52 return false; 53 } 54 if (a[i] !== b[i]) { 55 return false; 56 } 57 } 58 return true; 59} 60 61async function checkSoftware() { 62 let dsaAlies = "useDsaToCheckSoftware"; 63 let dsaOption = { 64 properties: new Array( 65 HuksSignVerifyDSA.HuksKeyAlgDSA, 66 HuksSignVerifyDSA.HuksKeyRSAPurposeSINGVERIFY, 67 HuksSignVerifyDSA.HuksKeySIZE1024, 68 HuksSignVerifyDSA.HuksTagDSADigestSHA256 69 ) 70 }; 71 try { 72 let res = await huks.generateKeyItem(dsaAlies, dsaOption); 73 if (res == null) { 74 await huks.deleteKeyItem(dsaAlies, dsaOption); 75 console.error("This device uses software"); 76 return true; 77 } else { 78 console.error("This device does not use software"); 79 return false; 80 } 81 } catch (e) { 82 return false; 83 } 84} 85 86export { stringToArray, uint8ArrayToString, stringToUint8Array, arrayEqual, checkSoftware }; 87