Skip to content

Commit bf51714

Browse files
committed
feat: convert md5 ArrayBuffer to Uint8Array
1 parent dfcee11 commit bf51714

File tree

1 file changed

+5
-12
lines changed
  • packages/pg-gateway/src/auth

1 file changed

+5
-12
lines changed

packages/pg-gateway/src/auth/md5.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type Md5AuthOptions = {
1414
credentials: {
1515
username: string;
1616
preHashedPassword: string;
17-
salt: BufferSource;
17+
salt: Uint8Array;
1818
hashedPassword: string;
1919
},
2020
connectionState: ConnectionState,
@@ -54,7 +54,7 @@ export class Md5AuthFlow extends BaseAuthFlow {
5454
this.salt = generateMd5Salt();
5555
}
5656

57-
async *handleClientMessage(message: BufferSource) {
57+
async *handleClientMessage(message: Uint8Array) {
5858
const length = this.reader.int32();
5959
const hashedPassword = this.reader.cstring();
6060

@@ -112,22 +112,15 @@ export class Md5AuthFlow extends BaseAuthFlow {
112112
*
113113
* @see https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-START-UP
114114
*/
115-
export async function hashPreHashedPassword(preHashedPassword: string, salt: BufferSource) {
116-
const hash = await md5(
117-
concat([
118-
new TextEncoder().encode(preHashedPassword),
119-
salt instanceof ArrayBuffer
120-
? new Uint8Array(salt)
121-
: new Uint8Array(salt.buffer, salt.byteOffset, salt.byteLength),
122-
]),
123-
);
115+
export async function hashPreHashedPassword(preHashedPassword: string, salt: Uint8Array) {
116+
const hash = await md5(concat([new TextEncoder().encode(preHashedPassword), salt]));
124117
return `md5${hash}`;
125118
}
126119

127120
/**
128121
* Computes the MD5 hash of the given value.
129122
*/
130-
export async function md5(value: string | BufferSource) {
123+
export async function md5(value: string | Uint8Array) {
131124
const hash = await crypto.subtle.digest(
132125
'MD5',
133126
typeof value === 'string' ? new TextEncoder().encode(value) : value,

0 commit comments

Comments
 (0)