refactor: replace MD5 utility with CryptoJS for password hashing
This commit is contained in:
parent
5910f134a2
commit
0a9c068f43
@ -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
|
||||
@ -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<CarInfoType>('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()
|
||||
|
||||
@ -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 = `<getExaminationStudentInfoReq><head><checkCode>${Md5.Instance.get_md5(this.carInfo.carId +
|
||||
let md5Message: string = CryptoJS.MD5(this.carInfo.carId +
|
||||
this.carInfo.examinationRoomId +
|
||||
this.examinerLoginInfo.username)}</checkCode></head><body><carId>${this.carInfo.carId}</carId><examinationRoomId>${this.carInfo.examinationRoomId}</examinationRoomId><examinerName>${this.examinerLoginInfo.username}</examinerName><sfzmhm></sfzmhm></body></getExaminationStudentInfoReq>`
|
||||
this.examinerLoginInfo.username).toString();
|
||||
const param =
|
||||
`<getExaminationStudentInfoReq><head><checkCode>${md5Message}</checkCode></head><body><carId>${this.carInfo.carId}</carId><examinationRoomId>${this.carInfo.examinationRoomId}</examinationRoomId><examinerName>${this.examinerLoginInfo.username}</examinerName><sfzmhm></sfzmhm></body></getExaminationStudentInfoReq>`
|
||||
try {
|
||||
getExaminationStudentInfo(param).then(async (res) => {
|
||||
console.log("temp log ", JSON.stringify(res))
|
||||
|
||||
@ -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
|
||||
@ -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",
|
||||
|
||||
@ -9,5 +9,7 @@
|
||||
"description": "Please describe the basic information.",
|
||||
"main": "",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {}
|
||||
"dependencies": {
|
||||
"@ohos/crypto-js": "2.0.3"
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user