Skip to content

Commit

Permalink
Added Base64Signature type (32)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Oct 23, 2024
1 parent cf7a5da commit 640290a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/controllers/UserLoginController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
} from '../util';
import type {
Base64Nonce,
Base64PrivateKey,
Base64PublicKey,
IAsyncCacheService,
Lazy,
ServerState,
Expand Down
1 change: 1 addition & 0 deletions src/types/commonTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type OperateAsUsername = Brand<'OperateAsUsername', string>;
export type Base64PrivateKey = Brand<'Base64PrivateKey', string>;
export type Base64PublicKey = Brand<'Base64PublicKey', string>;
export type Base64Nonce = Brand<'Base64Nonce', string>;
export type Base64Signature = Brand<'Base64Signature', string>;
export type DHPrivateKey = Brand<'DHPrivateKey', string>;
export type DHPublicKey = Brand<'DHPublicKey', string>;
export type ServerSecretKeys = Record<string, Base64PrivateKey>;
Expand Down
11 changes: 9 additions & 2 deletions src/util/authUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
Base64Nonce,
Base64PrivateKey,
Base64PublicKey,
Base64Signature,
DHPrivateKey,
DHPublicKey,
} from '../types';
Expand Down Expand Up @@ -58,16 +59,22 @@ export function formatDHPrivateKey(
].join('\n') as DHPrivateKey;
}

/**
* Sign a nonce using a private key.
* @param nonce
* @param privateKey
* @returns The base64 encoded signature.
*/
export function signWithPrivateKey(
nonce: Base64Nonce,
privateKey: Base64PrivateKey
): string {
): Base64Signature {
const nonceBytes = Buffer.from(nonce, 'base64');
const privateKeyBytes = Buffer.from(privateKey, 'base64');

return sign('sha256', nonceBytes, {
key: privateKeyBytes,
format: 'der',
type: 'pkcs8',
}).toString('base64');
}).toString('base64') as Base64Signature;
}

0 comments on commit 640290a

Please sign in to comment.