• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15
16import huks from '@ohos.security.huks';
17import { HuksSignVerifyDSA } from "./signverify/publicSignverifyParam";
18import Data from "../data.json";
19let srcData65 = Data.Data65b;
20
21function stringToArray(str) {
22  var arr = [];
23  for (var i = 0, j = str.length; i < j; ++i) {
24    arr.push(str.charCodeAt(i));
25  }
26  return arr;
27}
28
29function uint8ArrayToString(fileData) {
30  var dataString = '';
31  for (var i = 0; i < fileData.length; i++) {
32    dataString += String.fromCharCode(fileData[i]);
33  }
34  return dataString;
35}
36function stringToUint8Array(str) {
37  var arr = [];
38  for (var i = 0, j = str.length; i < j; ++i) {
39    arr.push(str.charCodeAt(i));
40  }
41  var tmpUint8Array = new Uint8Array(arr);
42  return tmpUint8Array;
43}
44function arrayEqual(a, b) {
45  if ((!a instanceof Array) || (!b instanceof Array)) {
46    return false;
47  }
48  if (a.length !== b.length) {
49    return false;
50  }
51  for (let i = 0; i < a.length; ++i) {
52    if ((!a instanceof Number) || (!b instanceof Number)) {
53      return false;
54    }
55    if (a[i] !== b[i]) {
56      return false;
57    }
58  }
59  return true;
60}
61
62async function checkSoftware() {
63  let dsaAlies = "useDsaToCheckSoftware";
64  let dsaOption = {
65    properties: new Array(
66      HuksSignVerifyDSA.HuksKeyAlgDSA,
67      HuksSignVerifyDSA.HuksKeyRSAPurposeSINGVERIFY,
68      HuksSignVerifyDSA.HuksKeySIZE1024,
69      HuksSignVerifyDSA.HuksTagDSADigestSHA256
70    )
71  };
72  let res = await huks.generateKey(dsaAlies, dsaOption);
73  console.log(`test generate:${JSON.stringify(res)}`);
74  if (res.errorCode == 0) {
75    await huks.deleteKey(dsaAlies, dsaOption);
76    console.error("This device uses software");
77    return true;
78  } else {
79    console.error("This device does not use software");
80    return false;
81  }
82}
83
84export { stringToArray, uint8ArrayToString, stringToUint8Array, arrayEqual, checkSoftware };
85