From 62c2f7b311da20cd9ca45a13e976d5665b00043a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aaron=20D=C3=B6tsch?= <aaron@fsmpi.rwth-aachen.de> Date: Wed, 10 May 2023 15:22:33 +0200 Subject: [PATCH] Shorten bulk voucher default length When bulk creating vouchers it was using a code length of 14 characters. Doing the math shows that with just 6 characters of length you need more than 33 thousand codes in order to have a 1% chance of collision (assuming I was using that online calculator correctly). That's more than enough. --- src/routes/admin/api/transactions.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/routes/admin/api/transactions.js b/src/routes/admin/api/transactions.js index 540b705..4d8be78 100644 --- a/src/routes/admin/api/transactions.js +++ b/src/routes/admin/api/transactions.js @@ -109,7 +109,8 @@ export async function editVoucher(id, code, amount, activated, expiresAt){ }); } -function generateRandomString(forbidden=new Set(), length=14, chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"){ +// >33k codes needed for a 1% chance of a collision +function generateRandomString(forbidden=new Set(), length=6, chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"){ let result = ""; for(let i = 0; i < length; i++){ result += chars.charAt(Math.floor(Math.random() * chars.length)); -- GitLab