From f7f3219becf168f4a210ed33fbd41363e0e9616e Mon Sep 17 00:00:00 2001 From: zhaodongyang Date: Fri, 21 Apr 2023 14:33:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0js-sha1=E7=9A=84XTS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaodongyang --- .gitignore | 3 +- entry/.gitignore | 3 +- entry/package.json | 4 +- entry/src/ohosTest/ets/test/JsSha1.test.ets | 136 ++++++++++++++++++++ entry/src/ohosTest/ets/test/List.test.ets | 2 + 5 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 entry/src/ohosTest/ets/test/JsSha1.test.ets diff --git a/.gitignore b/.gitignore index dc4e635..2abf9d6 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ .cxx /.clangd /.clang-format -/.clang-tidy \ No newline at end of file +/.clang-tidy +package-lock.json \ No newline at end of file diff --git a/entry/.gitignore b/entry/.gitignore index 5a6ba80..fbba509 100644 --- a/entry/.gitignore +++ b/entry/.gitignore @@ -1,4 +1,5 @@ /node_modules /.preview /build -/.cxx \ No newline at end of file +/.cxx +package-lock.json \ No newline at end of file diff --git a/entry/package.json b/entry/package.json index ca2095d..4bb87da 100644 --- a/entry/package.json +++ b/entry/package.json @@ -21,6 +21,8 @@ "js-sha1": "^0.6.0", "soundex": "^0.2.1", "js-md2": "^0.2.2", - "js-base64": "^3.7.2" + "js-base64": "^3.7.2", + "@ohos/crypto-js": "^1.0.6", + "@types/crypto-js": "^4.1.1" } } diff --git a/entry/src/ohosTest/ets/test/JsSha1.test.ets b/entry/src/ohosTest/ets/test/JsSha1.test.ets new file mode 100644 index 0000000..71d0072 --- /dev/null +++ b/entry/src/ohosTest/ets/test/JsSha1.test.ets @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' +// @ts-ignore +import sha1 from 'js-sha1' +import { CryptoJS } from '@ohos/crypto-js' +import buffer from '@ohos.buffer'; + +export default function jsSha1Test() { + describe('JsSha1Test', function () { + beforeAll(function () { + }) + beforeEach(function () { + }) + afterEach(function () { + }) + afterAll(function () { + }) + it('EmptyStringTest', 0, function () { + let result = sha1('') + expect(result).assertEqual('da39a3ee5e6b4b0d3255bfef95601890afd80709') + }) + it('LetterTest', 0, function () { + let result = sha1('The quick brown fox jumps over the lazy dog') + expect(result).assertEqual('2fd4e1c67a2d28fced849ee1bb76e7391b93eb12') + }) + it('LetterPunctuationTest', 0, function () { + let result = sha1('The quick brown fox jumps over the lazy dog.') + expect(result).assertEqual('408d94384216f890ff7a0c3528e8bed1e0b01621') + }) + it('ChineseLettersTest', 0, function () { + let result = sha1('中文') + expect(result).assertEqual('7be2d2d20c106eee0836c9bc2b939890a78e8fb3') + }) + it('EmptyArrayTest', 0, function () { + let result = sha1([]) + expect(result).assertEqual('da39a3ee5e6b4b0d3255bfef95601890afd80709') + }) + it('EmptyUin8ArrayTest', 0, function () { + let result = sha1(new Uint8Array([])) + expect(result).assertEqual('da39a3ee5e6b4b0d3255bfef95601890afd80709') + }) + it('HexTest', 0, function () { + let result = sha1.hex('') + expect(result).assertEqual('da39a3ee5e6b4b0d3255bfef95601890afd80709') + }) + it('ArrayTest', 0, function () { + let arr = [218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9]; + let result = sha1.array('') as Array + let matchResult = matchArry(arr, result) + expect(matchResult).assertTrue() + + }) + it('DigestTest', 0, function () { + let arr = [218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9] + let result = sha1.digest('') + let matchResult = matchArry(arr, result) + expect(matchResult).assertTrue() + }) + it('ArrayBufferTest', 0, function () { + let result = sha1.arrayBuffer('') + let type = '' + if (result instanceof ArrayBuffer) { + type = 'ArrayBuffer' + } + expect(type).assertEqual('ArrayBuffer') + }) + it('CompareWithCryptoJS_EmptyStringTest', 0, function () { + let result = sha1('') + var hash = CryptoJS.SHA1('') + expect(result).assertEqual(hash.toString()) + }) + it('CompareWithCryptoJS_LetterTest', 0, function () { + let result = sha1('The quick brown fox jumps over the lazy dog'); + var hash = CryptoJS.SHA1('The quick brown fox jumps over the lazy dog') + expect(result).assertEqual(hash.toString()) + }) + it('CompareWithCryptoJS_LetterPunctuationTest', 0, function () { + let result = sha1('The quick brown fox jumps over the lazy dog.'); + var hash = CryptoJS.SHA1('The quick brown fox jumps over the lazy dog.') + expect(result).assertEqual(hash.toString()) + }) + it('CompareWithCryptoJS_ChineseLettersTest', 0, function () { + let result = sha1('中文'); + var hash = CryptoJS.SHA1('中文') + expect(result).assertEqual(hash.toString()) + }) + it('CompareWithCryptoJS_EmptyArrayTest', 0, function () { + let result = sha1([]); + var hash = CryptoJS.SHA1(CryptoJS.lib.WordArray.create([])) + expect(result).assertEqual(hash.toString()) + }) + it('CompareWithCryptoJS_EmptyUin8ArrayTest', 0, function () { + let result = sha1(new Uint8Array([])); + let tempArr = Array.from(new Uint8Array([])) + var hash = CryptoJS.SHA1(CryptoJS.lib.WordArray.create(tempArr)) + expect(result).assertEqual(hash.toString()) + }) + it('CompareWithCryptoJS_HexTest', 0, function () { + var obj = sha1.create(); + obj.update('Message to hash'); + let result = obj.hex(); + var SHA1 = CryptoJS.algo.SHA1.create(); + SHA1.update('Message to hash'); + let hash = SHA1.finalize() + expect(result.toString()).assertEqual(hash.toString()) + }) + + }) + + function matchArry(arr1: Array, arr2: Array): boolean { + if (arr1.length == arr2.length) { + for (var i = 0; i < arr1.length; i++) { + if (arr1[i] != arr2[i]) { + return false; + } + } + return true; + } + return false; + } + +} \ No newline at end of file diff --git a/entry/src/ohosTest/ets/test/List.test.ets b/entry/src/ohosTest/ets/test/List.test.ets index 7ac6efd..b0769b1 100644 --- a/entry/src/ohosTest/ets/test/List.test.ets +++ b/entry/src/ohosTest/ets/test/List.test.ets @@ -13,7 +13,9 @@ * limitations under the License. */ import abilityTest from './Ability.test' +import jsSha1Test from './JsSha1.test' export default function testsuite() { abilityTest() + jsSha1Test() } \ No newline at end of file -- Gitee From 4a1e6c7540bc0127d0f406bb6097a4b0a7575bbe Mon Sep 17 00:00:00 2001 From: zhaodongyang Date: Fri, 21 Apr 2023 15:01:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8E=BB=E6=8E=89XTS=E4=B8=AD=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=AF=B9=E7=85=A7=E6=95=88=E6=9E=9C=E5=BC=95=E5=85=A5?= =?UTF-8?q?=E7=9A=84crypto-js=EF=BC=8C=E5=87=8F=E5=B0=91=E9=A2=9D=E5=A4=96?= =?UTF-8?q?=E4=B8=89=E6=96=B9=E5=BA=93=E7=9A=84=E5=BD=B1=E5=93=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaodongyang --- entry/package.json | 12 ++-- entry/src/ohosTest/ets/test/JsSha1.test.ets | 66 +++++++++------------ 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/entry/package.json b/entry/package.json index 4bb87da..5c1f779 100644 --- a/entry/package.json +++ b/entry/package.json @@ -11,18 +11,16 @@ "repository": {}, "version": "1.0.0", "dependencies": { + "urlencode": "^1.1.0", + "metaphone": "^2.0.0", + "js-sha1": "^0.6.0", + "js-base64": "^3.7.2", "soundex-code": "^2.0.0", "caverphone": "^1.0.0", "js-sha256": "^0.9.0", "hi-base32": "^0.5.1", "js-md5": "^0.7.3", - "urlencode": "^1.1.0", - "metaphone": "^2.0.0", - "js-sha1": "^0.6.0", "soundex": "^0.2.1", - "js-md2": "^0.2.2", - "js-base64": "^3.7.2", - "@ohos/crypto-js": "^1.0.6", - "@types/crypto-js": "^4.1.1" + "js-md2": "^0.2.2" } } diff --git a/entry/src/ohosTest/ets/test/JsSha1.test.ets b/entry/src/ohosTest/ets/test/JsSha1.test.ets index 71d0072..a6b4ae3 100644 --- a/entry/src/ohosTest/ets/test/JsSha1.test.ets +++ b/entry/src/ohosTest/ets/test/JsSha1.test.ets @@ -16,8 +16,6 @@ import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' // @ts-ignore import sha1 from 'js-sha1' -import { CryptoJS } from '@ohos/crypto-js' -import buffer from '@ohos.buffer'; export default function jsSha1Test() { describe('JsSha1Test', function () { @@ -78,45 +76,39 @@ export default function jsSha1Test() { } expect(type).assertEqual('ArrayBuffer') }) - it('CompareWithCryptoJS_EmptyStringTest', 0, function () { - let result = sha1('') - var hash = CryptoJS.SHA1('') - expect(result).assertEqual(hash.toString()) - }) - it('CompareWithCryptoJS_LetterTest', 0, function () { - let result = sha1('The quick brown fox jumps over the lazy dog'); - var hash = CryptoJS.SHA1('The quick brown fox jumps over the lazy dog') - expect(result).assertEqual(hash.toString()) - }) - it('CompareWithCryptoJS_LetterPunctuationTest', 0, function () { - let result = sha1('The quick brown fox jumps over the lazy dog.'); - var hash = CryptoJS.SHA1('The quick brown fox jumps over the lazy dog.') - expect(result).assertEqual(hash.toString()) - }) - it('CompareWithCryptoJS_ChineseLettersTest', 0, function () { - let result = sha1('中文'); - var hash = CryptoJS.SHA1('中文') - expect(result).assertEqual(hash.toString()) - }) - it('CompareWithCryptoJS_EmptyArrayTest', 0, function () { - let result = sha1([]); - var hash = CryptoJS.SHA1(CryptoJS.lib.WordArray.create([])) - expect(result).assertEqual(hash.toString()) - }) - it('CompareWithCryptoJS_EmptyUin8ArrayTest', 0, function () { - let result = sha1(new Uint8Array([])); - let tempArr = Array.from(new Uint8Array([])) - var hash = CryptoJS.SHA1(CryptoJS.lib.WordArray.create(tempArr)) - expect(result).assertEqual(hash.toString()) + it('StringTest', 0, function () { + let result = sha1('13579ABCDEabcde,./;‘【') + expect(result).assertEqual('33c87d9da1d745679003dee486fad3a8169f3862') + }) + it('UppercaseLowerCaseLetterTest', 0, function () { + let result = sha1('THE quick brown FOX jumps over 123 lazy dog'); + expect(result).assertEqual('c59942070e96a123532e4b381de17af0136949a1') + }) + it('AlltypeTest', 0, function () { + let result = sha1('al25scAKF86.;*/【】!@#¥%你還得法國德國256'); + expect(result).assertEqual('30c49442129b32047842a7501d73e738a89b17c5') + }) + it('SimplifiedAndTraditionalChineseTest', 0, function () { + let result = sha1('中文简体繁體測試'); + expect(result).assertEqual('3352ea0b083302e43d0c0fc58ac993ae18e43d39') + }) + it('NotEmptyArrayTest', 0, function () { + let result = sha1([1,2,3,4]); + expect(result).assertEqual('12dada1fff4d4787ade3333147202c3b443e376f') + }) + it('StringUint8ArrayTest', 0, function () { + let result = sha1(new Uint8Array([1,2,3,4])); + expect(result).assertEqual('12dada1fff4d4787ade3333147202c3b443e376f') }) it('CompareWithCryptoJS_HexTest', 0, function () { var obj = sha1.create(); obj.update('Message to hash'); - let result = obj.hex(); - var SHA1 = CryptoJS.algo.SHA1.create(); - SHA1.update('Message to hash'); - let hash = SHA1.finalize() - expect(result.toString()).assertEqual(hash.toString()) + let result1 = obj.hex(); + + var result2 = sha1.hex('Message to hash') + + expect(result1.toString()).assertEqual('ad8b1ef19620cf94a1c0c9d6c87e7d1b2bca2584') + expect(result2.toString()).assertEqual('ad8b1ef19620cf94a1c0c9d6c87e7d1b2bca2584') }) }) -- Gitee