-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.ts
82 lines (71 loc) · 2.05 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Md5 } from "./deps.ts";
const encoder = new TextEncoder();
const decoder = new TextDecoder();
export function md5(str: string | ArrayBuffer): string {
return new Md5().update(str).toString();
}
export function Md5Password(
user: string,
password: string,
salt: Uint8Array,
): string {
const val = md5(password + user);
const md5WithSalt = md5(concatUint8Array([encoder.encode(val), salt]));
return "md5" + md5WithSalt;
}
export async function readMsg(reader: Deno.Reader): Promise<Uint8Array | null> {
const arr: Uint8Array[] = [];
const n = 1000;
let readed: number | null;
while (true) {
const p: Uint8Array = new Uint8Array(n);
readed = await reader.read(p);
if (readed === null) break;
if (readed < n) {
arr.push(p.subarray(0, readed));
break;
} else {
arr.push(p);
}
}
if (readed === null) return readed;
const result = concatUint8Array(arr);
return result;
}
export function concatUint8Array(arr: Uint8Array[]): Uint8Array {
const length = arr.reduce((pre, next) => pre + next.length, 0);
const result = new Uint8Array(length);
let offset = 0;
for (const v of arr) {
result.set(v, offset);
offset += v.length;
}
return result;
}
export function readUInt16BE(buffer: Uint8Array, offset: number = 0): number {
offset = offset >>> 0;
return buffer[offset] | (buffer[offset + 1] << 8);
}
export function readInt16BE(buffer: Uint8Array, offset: number): number {
offset = offset >>> 0;
const val = buffer[offset + 1] | (buffer[offset] << 8);
return val & 0x8000 ? val | 0xffff0000 : val;
}
export function readInt32BE(buffer: Uint8Array, offset: number = 0): number {
offset = offset >>> 0;
return (
(buffer[offset] << 24) |
(buffer[offset + 1] << 16) |
(buffer[offset + 2] << 8) |
buffer[offset + 3]
);
}
export function readUInt32BE(buffer: Uint8Array, offset: number = 0): number {
offset = offset >>> 0;
return (
buffer[offset] * 0x1000000 +
((buffer[offset + 1] << 16) |
(buffer[offset + 2] << 8) |
buffer[offset + 3])
);
}