diff --git a/CHANGELOG.md b/CHANGELOG.md index 53c9716b9ed4fc02b470c0c62383eafde5c5185d..8f3be31da57daeabd3c6a645d35f783d6307f099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v1.0.3 + +1. 修复加密算法中特殊字符以及中文字符加密错误的问题 + ## v1.0.2 1. 适配API9 diff --git a/crypto/package.json b/crypto/package.json index 18222718cfcc191d20685176b97bc8fc51a5f833..51936f0ab124cd8f47914a3a5b58c095df10ac12 100644 --- a/crypto/package.json +++ b/crypto/package.json @@ -11,12 +11,13 @@ }, "main": "index.ets", "repository": "https://gitee.com/openharmony-sig/crypto-js", - "version": "1.0.2", + "version": "1.0.3", "tags": [ "crypto-js", "OpenHarmony" ], "dependencies": { + "@ohos/openharmony-node-polyfill": "^0.1.0", "crypto-js": "^4.1.1" }, "license": "MIT", diff --git a/crypto/src/main/ets/UnescapeBuffer.ts b/crypto/src/main/ets/UnescapeBuffer.ts new file mode 100644 index 0000000000000000000000000000000000000000..5640df5903c985d0802d599fc1ea102277c1b9bb --- /dev/null +++ b/crypto/src/main/ets/UnescapeBuffer.ts @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the MIT License, (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * 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 buffer from '@ohos.buffer'; + +const unhexTable = new Int8Array([ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0 - 15 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16 - 31 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32 - 47 + +0, +1, +2, +3, +4, +5, +6, +7, +8, +9, -1, -1, -1, -1, -1, -1, // 48 - 63 + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 64 - 79 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80 - 95 + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 96 - 111 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 112 - 127 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128 ... + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 255 +]); + +export default class UnescapeBuffer { + /** + * A safe fast alternative to decodeURIComponent + * @param {string} s + * @param {boolean} decodeSpaces + * @returns {string} + */ + unescapeBuffer(s, decodeSpaces) { + const out = buffer.alloc(s.length); + let index = 0; + let outIndex = 0; + let currentChar; + let nextChar; + let hexHigh; + let hexLow; + const maxLength = s.length - 2; + // Flag to know if some hex chars have been decoded + let hasHex = false; + while (index < s.length) { + currentChar = s.charCodeAt(index) + if (currentChar === 43 /* '+' */ + && decodeSpaces) { + out[outIndex++] = 32; // ' ' + index++; + continue; + } + if (currentChar === 37 /* '%' */ + && index < maxLength) { + currentChar = s.charCodeAt(++index); + hexHigh = unhexTable[currentChar]; + if (!(hexHigh >= 0)) { + out[outIndex++] = 37; // '%' + continue; + } else { + nextChar = s.charCodeAt(++index); + hexLow = unhexTable[nextChar]; + if (!(hexLow >= 0)) { + out[outIndex++] = 37; // '%' + index--; + } else { + hasHex = true; + currentChar = hexHigh * 16 + hexLow; + } + } + } + out[outIndex++] = currentChar; + index++; + } + return hasHex ? out.subarray(0, outIndex) : out; + } +} diff --git a/crypto/src/main/ets/crypto.ts b/crypto/src/main/ets/crypto.ts index d964133357d5136bad051d6167ef27239397e98a..a45d83982b67c7518f7e7805672a79fbf48ff415 100644 --- a/crypto/src/main/ets/crypto.ts +++ b/crypto/src/main/ets/crypto.ts @@ -14,6 +14,8 @@ */ import CryptoJS from 'crypto-js'; +import OpenHarmonyBuffer from "@ohos/openharmony-node-polyfill/lib/node/buffer" +import UnescapeBuffer from "./UnescapeBuffer" export { CryptoJS } @@ -45,12 +47,12 @@ CryptoJS.lib.WordArray.random = function (nBytes) { CryptoJS.enc.Utf8.stringify = function (wordArray) { try { - return decodeURI(CryptoJS.enc.Latin1.stringify(wordArray)); + return OpenHarmonyBuffer.Buffer.from(CryptoJS.enc.Latin1.stringify(wordArray), "Latin1").toString("utf-8"); } catch (e) { throw new Error('Malformed UTF-8 data'); } } CryptoJS.enc.Utf8.parse = function (utf8Str) { - return CryptoJS.enc.Latin1.parse(encodeURI(utf8Str)); -} + return CryptoJS.enc.Latin1.parse(UnescapeBuffer.prototype.unescapeBuffer(encodeURIComponent(utf8Str), false).toString("binary")); +} \ No newline at end of file