Skip to content

Commit

Permalink
Increase auth nonce entropy (#1383)
Browse files Browse the repository at this point in the history
Increases the nonce entropy the auth implementation to match that of the official SiWe implementation (18 characters).
  • Loading branch information
iamacook authored Apr 10, 2024
1 parent 4d143d0 commit 3358128
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/datasources/auth-api/siwe-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@ import { verifyMessage } from 'viem';

@Injectable()
export class SiweApi implements IAuthApi {
private static readonly NONCE_LENGTH = 8;
/**
* The official SiWe implementation uses a nonce length of 17:
*
* > 96 bits has been chosen as a number to sufficiently balance size and security
* > considerations relative to the lifespan of it's usage.
*
* ```
* const ALPHANUMERIC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
* const length = Math.ceil(96 / (Math.log(ALPHANUMERIC.length) / Math.LN2)) // 17
* ```
*
* @see https://github.com/spruceid/siwe/blob/0e63b05cd3c722abd282dd1128aa8878648a8620/packages/siwe/lib/utils.ts#L36-L53
* @see https://github.com/StableLib/stablelib/blob/5243520e343c217b6a751464dec1bc980cb510d8/packages/random/random.ts#L80-L99
*
* As we rely on typed arrays to generate random values, we must use an even number.
* We therefore use a length of 18 to be compatible and remain as similar as possible.
*/
private static readonly NONCE_LENGTH = 18;

constructor(
@Inject(LoggingService)
Expand Down

0 comments on commit 3358128

Please sign in to comment.