From b636afc4bd411e3c378218cae57e5ef241dbbb4a Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Mon, 14 Jun 2021 08:45:48 +0200 Subject: [PATCH 1/2] fix(publicId): generate 128-bit instead of 128-byte value Signed-off-by: Philip Molares --- src/notes/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/notes/utils.ts b/src/notes/utils.ts index cbb907171..9f8f87153 100644 --- a/src/notes/utils.ts +++ b/src/notes/utils.ts @@ -12,6 +12,6 @@ import { randomBytes } from 'crypto'; * This is a randomly generated 128-bit value encoded with base32-encode using the crockford variant and converted to lowercase. */ export function generatePublicId(): string { - const randomId = randomBytes(128); + const randomId = randomBytes(16); return base32Encode(randomId, 'Crockford').toLowerCase(); } From eea0e410d34c77e81652857e1ba6bea15b76d478 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Tue, 15 Jun 2021 21:17:07 +0200 Subject: [PATCH 2/2] test(generatePublicId): test if with a given random buffer a correct encoding is generated Signed-off-by: Philip Molares --- src/notes/utils.spec.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/notes/utils.spec.ts diff --git a/src/notes/utils.spec.ts b/src/notes/utils.spec.ts new file mode 100644 index 000000000..0666e55d1 --- /dev/null +++ b/src/notes/utils.spec.ts @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { randomBytes } from 'crypto'; +import { generatePublicId } from './utils'; +jest.mock('crypto'); + +it('generatePublicId', () => { + const random128bitBuffer = Buffer.from([ + 0xe1, 0x75, 0x86, 0xb7, 0xc3, 0xfb, 0x03, 0xa9, 0x26, 0x9f, 0xc9, 0xd6, + 0x8c, 0x2d, 0x7b, 0x7b, + ]); + const mockRandomBytes = randomBytes as jest.MockedFunction< + typeof randomBytes + >; + mockRandomBytes.mockImplementationOnce((_) => random128bitBuffer); + + expect(generatePublicId()).toEqual('w5trddy3zc1tj9mzs7b8rbbvfc'); +});