From 0a9c068f43075728b8cd0d3b02f52a393b8c20ae Mon Sep 17 00:00:00 2001 From: wangzhongjie Date: Thu, 27 Mar 2025 10:30:21 +0800 Subject: [PATCH] refactor: replace MD5 utility with CryptoJS for password hashing --- entry/src/main/ets/common/utils/md5.ts | 167 --------------- entry/src/main/ets/pages/ExaminerLogin.ets | 6 +- entry/src/main/ets/pages/UserInfo.ets | 8 +- entry/src/main/ets/utils/MD5.ets | 227 --------------------- oh-package-lock.json5 | 8 + oh-package.json5 | 4 +- 6 files changed, 20 insertions(+), 400 deletions(-) delete mode 100644 entry/src/main/ets/common/utils/md5.ts delete mode 100644 entry/src/main/ets/utils/MD5.ets diff --git a/entry/src/main/ets/common/utils/md5.ts b/entry/src/main/ets/common/utils/md5.ts deleted file mode 100644 index 5c8cc22a..00000000 --- a/entry/src/main/ets/common/utils/md5.ts +++ /dev/null @@ -1,167 +0,0 @@ -class Md5 { - private static _inst: Md5; - public static get Instance(): Md5 { - return this._inst || (this._inst = new Md5()); - } - public get_md5(str: string): string { - return this.md5(str); - } - split(target: string | any[], step: number, markString: boolean = typeof target === "string") { - if (typeof target === "string") target = target.split(""); - let result: any[] = target.map( - (_, index: number) => - index % step === 0 - ? Array.from(Array(step).keys()).map((x: number) => target[index + x]) - : [] - ) - .filter((x: any[]) => x.length > 0); - if (markString) result = result.map((x: any[]) => x.join("")) - return result; - } - padding(str: string | any[], length: number, char: string, tail: boolean = true, isArray: boolean = Array.isArray(str)) { - let arr; - if (Array.isArray(str)) { - arr = str; - } else { - arr = str.split(""); - } - const paddingStr: any[] = this.range(length - str.length).map(() => char); - const result = tail ? arr.concat(paddingStr) : paddingStr.concat(arr); - return isArray ? result : result.join(""); - } - little_endian(charCode: number) { - return this.split(this.padding(charCode.toString(16), 8, "0", false), 2).reverse().join(""); - } - range(...args: number[]) { - const start: number = args.length === 1 ? 0 : args[0]; - const end: number = args.length === 2 ? args[1] : args[0] - 1; - return Array.from(Array(end - start + 1).keys()).map((x: number) => x + start); - } - to_binary(code: number, bit: number = 8, max: number = Math.pow(2, bit) - 1) { - if (code < 0) throw new Error("code should be greater than: 0"); - if (code > max) throw new Error("code should be less than: " + max); - return this.padding(code.toString(2), bit, "0", false); - } - to_hex(code: number, bit: number = 8, max: number = Math.pow(16, bit) - 1) { - if (code < 0) throw new Error("code should be greater than: 0"); - if (code > max) throw new Error("code should be less than: " + max); - return this.padding(code.toString(16), bit, "0", false); - } - to_code(str: string) { - if (str.substr(0, 2).toLowerCase() === "0b") return parseInt(str.substr(2, 8), 2); - if (str.substr(0, 2).toLowerCase() === "0x") return parseInt(str.substr(2, 8), 16); - } - utf16_to_utf8(str: string) { - return str.split("").map((char: string) => this.utf8_encode(char)).join(""); - } - utf8_encode(char: string) { - let utftext = ""; - const c = char.charCodeAt(0); - if (c < 128) { - utftext += String.fromCharCode(c); - } else if ((c > 127) && (c < 2048)) { - utftext += String.fromCharCode((c >> 6) | 0b11000000); - utftext += String.fromCharCode((c & 0b00111111) | 0b10000000); - } else { - utftext += String.fromCharCode((c >> 12) | 0b11100000); - utftext += String.fromCharCode(((c >> 6) & 0b00111111) | 0b10000000); - utftext += String.fromCharCode((c & 0b00111111) | 0b10000000); - } - return utftext; - } - uint_add(...args: number[]) { - const t = Uint32Array.from([0]); - const x = Uint32Array.from(args); - x.forEach(n => t[0] = t[0] + n); - return t[0]; - } - loop_shift_left(n: number, bits: number) { - return (n << bits) | (n >>> (32 - bits)); - } - A = 0x67452301; - B = 0xefcdab89; - C = 0x98badcfe; - D = 0x10325476; - F(b: number, c: number, d: number) { - return (b & c) | (~b & d); - } - G(b: number, c: number, d: number) { - return (b & d) | (c & ~d); - } - H(b: number, c: number, d: number) { - return b ^ c ^ d; - } - I(b: number, c: number, d: number) { - return c ^ (b | ~d) - } - S = [ - 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, - 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, - 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, - 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 - ] - T(i: number) { - return Math.floor(Math.pow(2, 32) * Math.abs(Math.sin(i + 1))); - } - x_index(i: number) { - if (i >= 0 && i <= 15) return i; - if (i >= 16 && i <= 31) return (5 * i + 1) % 16; - if (i >= 32 && i <= 47) return (3 * i + 5) % 16; - if (i >= 48 && i <= 63) return (7 * i) % 16; - return 0; - } - wrap(m: any) { - return (a: number, b: number, c: number, d: number, x: number, s: number, t: number) => { - // 循环左移 - return this.uint_add(this.loop_shift_left(this.uint_add(a, m(b, c, d), x, t), s), b); - }; - } - porcess_message(str: string) { - const length = str.length; - const length_of_zero = Math.ceil(length / 64) * 64 - length - 8 - 1; - str += String.fromCharCode(0b10000000); - const strArray = this.padding(str.split(""), length + 1 + length_of_zero, String.fromCharCode(0)); - const tail = this.split(this.padding(this.to_binary(length * 8 % Math.pow(2, 64)), 64, "0"), 8).map(x => parseInt(x, 2)); - const head = (strArray as any[]).map(x => x.charCodeAt(0)); - return Uint32Array.from( - this.split(head.concat(tail), 4) - .map(x => - x.map((t: number) => this.padding(t.toString(16), 2, "0", false)).join("") - ) - .map(x => parseInt(x, 16)) - .map(x => parseInt(this.little_endian(x), 16)) - ) - } - fghi(i: number) { - if (i >= 0 && i <= 15) return this.F; - if (i >= 16 && i <= 31) return this.G; - if (i >= 32 && i <= 47) return this.H; - if (i >= 48 && i <= 63) return this.I; - } - fghi_wrapped(i: number) { - return this.wrap(this.fghi(i)); - } - //------------------------------------------------ - md5(str: string) { - str = this.utf16_to_utf8(str); - const uint32_array = this.porcess_message(str); - const result = Uint32Array.from([this.A, this.B, this.C, this.D]); - const chunks = this.split(Array.from(uint32_array), 16); - for (const chunk of chunks) { - const a = result[0]; - const b = result[1]; - const c = result[2]; - const d = result[3]; - for (let i = 0; i < 64; i++) { - result[(4 - i % 4) % 4] = this.fghi_wrapped(i)(result[(4 - i % 4) % 4], result[((4 - i % 4) + 1) % 4], result[((4 - i % 4) + 2) % 4], result[((4 - i % 4) + 3) % 4], chunk[this.x_index(i)], this.S[i], this.T(i)) - } - result[0] = a + result[0]; - result[1] = b + result[1]; - result[2] = c + result[2]; - result[3] = d + result[3]; - } - return Array.from(result).map(x => this.little_endian(x)).join("").toLowerCase(); - } -} - -export default Md5 \ No newline at end of file diff --git a/entry/src/main/ets/pages/ExaminerLogin.ets b/entry/src/main/ets/pages/ExaminerLogin.ets index 92af74d7..5387b13b 100644 --- a/entry/src/main/ets/pages/ExaminerLogin.ets +++ b/entry/src/main/ets/pages/ExaminerLogin.ets @@ -1,6 +1,5 @@ import router from '@ohos.router'; import TopLogo from './compontents/TopLogo'; -import Md5 from '../common/utils/md5'; import promptAction from '@ohos.promptAction'; import errorMsgDialog from './compontents/errorMsgDialog'; import imageBtn from './compontents/imageBtn'; @@ -8,6 +7,7 @@ import { UserLoginType } from '../model/ExaminerLogin'; import { BusinessError } from '@ohos.base'; import { examinerLogin } from '../api/login'; import { CarInfoType, ExaminerLoginInfo } from '../model/index'; +import { CryptoJS } from '@ohos/crypto-js'; @Entry @@ -95,11 +95,13 @@ struct Index { return } const carInfo: CarInfoType = AppStorage.get('carInfo') + let password: string = CryptoJS.MD5(this.inputTextArr[1]).toString(); const param: UserLoginType = { carId: carInfo.carId as string, examinationRoomId: carInfo.examinationRoomId as string, username: this.inputTextArr[0], - password: Md5.Instance.get_md5(this.inputTextArr[1]) + // password: Md5.Instance.get_md5(this.inputTextArr[1]) + password } this.type = '2' this.errorDialog.open() diff --git a/entry/src/main/ets/pages/UserInfo.ets b/entry/src/main/ets/pages/UserInfo.ets index b9e10832..35ae7a2b 100644 --- a/entry/src/main/ets/pages/UserInfo.ets +++ b/entry/src/main/ets/pages/UserInfo.ets @@ -1,7 +1,6 @@ import { examinationStuAbsent, getExaminationItem, getExaminationStudentInfo } from '../api/userInfo'; import router from '@ohos.router'; import TopLogo from './compontents/TopLogo'; -import Md5 from '../common/utils/md5'; import FaceCompare from './compontents/FaceCompare'; import { writeObjectOut } from '../api/judge'; import testNapi from '@ohos.idcard'; @@ -18,6 +17,7 @@ import { judgeConfig } from './judgeSDK/utils/judgeConfig'; import { initJudgeUdp } from '../common/utils/UdpJudge'; import errorMsgDialog from './compontents/errorMsgDialog'; import imageBtn from './compontents/imageBtn'; +import { CryptoJS } from '@ohos/crypto-js'; import { BeginExamRequest, @@ -664,9 +664,11 @@ struct UserInfo { this.type = '2' this.errorDialog.open() this.updateTimeLimit = true - const param = `${Md5.Instance.get_md5(this.carInfo.carId + + let md5Message: string = CryptoJS.MD5(this.carInfo.carId + this.carInfo.examinationRoomId + - this.examinerLoginInfo.username)}${this.carInfo.carId}${this.carInfo.examinationRoomId}${this.examinerLoginInfo.username}` + this.examinerLoginInfo.username).toString(); + const param = + `${md5Message}${this.carInfo.carId}${this.carInfo.examinationRoomId}${this.examinerLoginInfo.username}` try { getExaminationStudentInfo(param).then(async (res) => { console.log("temp log ", JSON.stringify(res)) diff --git a/entry/src/main/ets/utils/MD5.ets b/entry/src/main/ets/utils/MD5.ets deleted file mode 100644 index 233f11f6..00000000 --- a/entry/src/main/ets/utils/MD5.ets +++ /dev/null @@ -1,227 +0,0 @@ -class Md5 { - private static _inst: Md5; - A = 0x67452301; - B = 0xefcdab89; - C = 0x98badcfe; - D = 0x10325476; - S = [ - 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, - 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, - 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, - 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 - ] - - public static get Instance(): Md5 { - return this._inst || (this._inst = new Md5()); - } - - public get_md5(str: string): string { - return this.md5(str); - } - - split(target: string | any[], step: number, markString: boolean = typeof target === "string") { - if (typeof target === "string") { - target = target.split(""); - } - let result: any[] = target.map( - (_, index: number) => - index % step === 0 - ? Array.from(Array(step).keys()).map((x: number) => target[index + x]) - : [] - ) - .filter((x: any[]) => x.length > 0); - if (markString) { - result = result.map((x: any[]) => x.join("")) - } - return result; - } - - padding(str: string | any[], length: number, char: string, tail: boolean = true, - isArray: boolean = Array.isArray(str)) { - let arr; - if (Array.isArray(str)) { - arr = str; - } else { - arr = str.split(""); - } - const paddingStr: any[] = this.range(length - str.length).map(() => char); - const result = tail ? arr.concat(paddingStr) : paddingStr.concat(arr); - return isArray ? result : result.join(""); - } - - little_endian(charCode: number) { - return this.split(this.padding(charCode.toString(16), 8, "0", false), 2).reverse().join(""); - } - - range(...args: number[]) { - const start: number = args.length === 1 ? 0 : args[0]; - const end: number = args.length === 2 ? args[1] : args[0] - 1; - return Array.from(Array(end - start + 1).keys()).map((x: number) => x + start); - } - - to_binary(code: number, bit: number = 8, max: number = Math.pow(2, bit) - 1) { - if (code < 0) { - throw new Error("code should be greater than: 0"); - } - if (code > max) { - throw new Error("code should be less than: " + max); - } - return this.padding(code.toString(2), bit, "0", false); - } - - to_hex(code: number, bit: number = 8, max: number = Math.pow(16, bit) - 1) { - if (code < 0) { - throw new Error("code should be greater than: 0"); - } - if (code > max) { - throw new Error("code should be less than: " + max); - } - return this.padding(code.toString(16), bit, "0", false); - } - - to_code(str: string) { - if (str.substr(0, 2).toLowerCase() === "0b") { - return parseInt(str.substr(2, 8), 2); - } - if (str.substr(0, 2).toLowerCase() === "0x") { - return parseInt(str.substr(2, 8), 16); - } - } - - utf16_to_utf8(str: string) { - return str.split("").map((char: string) => this.utf8_encode(char)).join(""); - } - - utf8_encode(char: string) { - let utftext = ""; - const c = char.charCodeAt(0); - if (c < 128) { - utftext += String.fromCharCode(c); - } else if ((c > 127) && (c < 2048)) { - utftext += String.fromCharCode((c >> 6) | 0b11000000); - utftext += String.fromCharCode((c & 0b00111111) | 0b10000000); - } else { - utftext += String.fromCharCode((c >> 12) | 0b11100000); - utftext += String.fromCharCode(((c >> 6) & 0b00111111) | 0b10000000); - utftext += String.fromCharCode((c & 0b00111111) | 0b10000000); - } - return utftext; - } - - uint_add(...args: number[]) { - const t = Uint32Array.from([0]); - const x = Uint32Array.from(args); - x.forEach(n => t[0] = t[0] + n); - return t[0]; - } - - loop_shift_left(n: number, bits: number) { - return (n << bits) | (n >>> (32 - bits)); - } - - F(b: number, c: number, d: number) { - return (b & c) | (~b & d); - } - - G(b: number, c: number, d: number) { - return (b & d) | (c & ~d); - } - - H(b: number, c: number, d: number) { - return b ^ c ^ d; - } - - I(b: number, c: number, d: number) { - return c ^ (b | ~d) - } - - T(i: number) { - return Math.floor(Math.pow(2, 32) * Math.abs(Math.sin(i + 1))); - } - - x_index(i: number) { - if (i >= 0 && i <= 15) { - return i; - } - if (i >= 16 && i <= 31) { - return (5 * i + 1) % 16; - } - if (i >= 32 && i <= 47) { - return (3 * i + 5) % 16; - } - if (i >= 48 && i <= 63) { - return (7 * i) % 16; - } - return 0; - } - - wrap(m: any) { - return (a: number, b: number, c: number, d: number, x: number, s: number, t: number) => { - // 循环左移 - return this.uint_add(this.loop_shift_left(this.uint_add(a, m(b, c, d), x, t), s), b); - }; - } - - porcess_message(str: string) { - const length = str.length; - const length_of_zero = Math.ceil(length / 64) * 64 - length - 8 - 1; - str += String.fromCharCode(0b10000000); - const strArray = this.padding(str.split(""), length + 1 + length_of_zero, String.fromCharCode(0)); - const tail = - this.split(this.padding(this.to_binary(length * 8 % Math.pow(2, 64)), 64, "0"), 8).map(x => parseInt(x, 2)); - const head = (strArray as any[]).map(x => x.charCodeAt(0)); - return Uint32Array.from( - this.split(head.concat(tail), 4) - .map(x => - x.map((t: number) => this.padding(t.toString(16), 2, "0", false)).join("") - ) - .map(x => parseInt(x, 16)) - .map(x => parseInt(this.little_endian(x), 16)) - ) - } - - fghi(i: number) { - if (i >= 0 && i <= 15) { - return this.F; - } - if (i >= 16 && i <= 31) { - return this.G; - } - if (i >= 32 && i <= 47) { - return this.H; - } - if (i >= 48 && i <= 63) { - return this.I; - } - } - - fghi_wrapped(i: number) { - return this.wrap(this.fghi(i)); - } - - //------------------------------------------------ - md5(str: string) { - str = this.utf16_to_utf8(str); - const uint32_array = this.porcess_message(str); - const result = Uint32Array.from([this.A, this.B, this.C, this.D]); - const chunks = this.split(Array.from(uint32_array), 16); - for (const chunk of chunks) { - const a = result[0]; - const b = result[1]; - const c = result[2]; - const d = result[3]; - for (let i = 0; i < 64; i++) { - result[(4 - i % 4) % 4] = - this.fghi_wrapped(i)(result[(4 - i % 4) % 4], result[((4 - i % 4) + 1) % 4], result[((4 - i % 4) + 2) % 4], - result[((4 - i % 4) + 3) % 4], chunk[this.x_index(i)], this.S[i], this.T(i)) - } - result[0] = a + result[0]; - result[1] = b + result[1]; - result[2] = c + result[2]; - result[3] = d + result[3]; - } - return Array.from(result).map(x => this.little_endian(x)).join("").toLowerCase(); - } -} - -export default Md5 \ No newline at end of file diff --git a/oh-package-lock.json5 b/oh-package-lock.json5 index 308b29d9..8a446ad6 100644 --- a/oh-package-lock.json5 +++ b/oh-package-lock.json5 @@ -5,9 +5,17 @@ "lockfileVersion": 3, "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", "specifiers": { + "@ohos/crypto-js@2.0.3": "@ohos/crypto-js@2.0.3", "@ohos/hypium@1.0.6": "@ohos/hypium@1.0.6" }, "packages": { + "@ohos/crypto-js@2.0.3": { + "name": "@ohos/crypto-js", + "version": "2.0.3", + "integrity": "sha512-LuHaR1kD5PxnOXnuR1fWvPwGtbed9Q/QGzk6JOh8y5Wdzvi8brPesODZiaWf9scOVRHsbTPOtZw91vWB35p1vQ==", + "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/crypto-js/-/crypto-js-2.0.3.har", + "registryType": "ohpm" + }, "@ohos/hypium@1.0.6": { "name": "@ohos/hypium", "version": "1.0.6", diff --git a/oh-package.json5 b/oh-package.json5 index 0b09f95a..778031bb 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -9,5 +9,7 @@ "description": "Please describe the basic information.", "main": "", "version": "1.0.0", - "dependencies": {} + "dependencies": { + "@ohos/crypto-js": "2.0.3" + } } \ No newline at end of file