refactor(otp-generator): coerce lowercase secret characters to uppercase

This commit is contained in:
Corentin Thomasset 2023-03-26 22:40:43 +02:00 committed by Corentin THOMASSET
parent f3b1863f09
commit 7c40539ef9
3 changed files with 7 additions and 2 deletions

View file

@ -96,7 +96,7 @@ const { attrs: secretValidationAttrs } = useValidation({
rules: [
{
message: 'Secret should be a base32 string',
validator: (value) => value.match(/^[A-Z234567]+$/),
validator: (value) => value.toUpperCase().match(/^[A-Z234567]+$/),
},
{
message: 'Please set a secret',

View file

@ -20,12 +20,16 @@ describe('otp functions', () => {
expect(hexToBytes('0102030405060708090a0b0c0d0e0f')).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
});
});
describe('base32tohex', () => {
describe('base32toHex', () => {
it('convert a base32 to hex string', () => {
expect(base32toHex('ABCDEF')).to.eql('00443205');
expect(base32toHex('7777')).to.eql('ffff0f');
expect(base32toHex('JBSWY3DPEHPK3PXP')).to.eql('48656c6c6f21deadbeef');
});
it('case does not matter', () => {
expect(base32toHex('ABC')).to.eql(base32toHex('abc'));
});
});
describe('generateHOTP', () => {

View file

@ -26,6 +26,7 @@ function base32toHex(base32: string) {
const base32Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
const bits = base32
.toUpperCase() // Since base 32, we coerce lowercase to uppercase
.replace(/=+$/, '')
.split('')
.map((value) => base32Chars.indexOf(value).toString(2).padStart(5, '0'))