Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key encoding option (ASCII) #150

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,22 @@ describe("totp generation", () => {
await expect(TOTP.generate("JBSWY3DPEHPK3PXP")).resolves.toEqual(expect.objectContaining({ otp: "282760" }))
})
})

describe("TOTP generation with ASCII keys", () => {
beforeEach(() => jest.useFakeTimers())
afterEach(() => jest.resetAllMocks())

test.each([
{ time: 59, expectedOtp: "94287082" },
{ time: 1111111109, expectedOtp: "07081804" },
{ time: 1111111111, expectedOtp: "14050471" },
{ time: 1234567890, expectedOtp: "89005924" },
{ time: 2000000000, expectedOtp: "69279037" },
{ time: 20000000000, expectedOtp: "65353130" },
])("should generate correct OTP for time $time", async ({ time, expectedOtp }) => {
jest.setSystemTime(time * 1000) // Convert seconds to milliseconds
await expect(TOTP.generate("12345678901234567890", { encoding: "ascii", digits: 8 })).resolves.toEqual(
expect.objectContaining({ otp: expectedOtp })
);
});
});
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
export type TOTPAlgorithm = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512"
export type TOTPEncoding = "hex" | "ascii"

/**
* Options for TOTP generation.
* @param {number} [digits=6] - The number of digits in the OTP.
* @param {TOTPAlgorithm} [algorithm="SHA-1"] - Algorithm used for hashing.
* @param {TOTPEncoding} [encoding="hex"] - Encoding used for the OTP.
* @param {number} [period=30] - The time period for OTP validity in seconds.
* @param {number} [timestamp=Date.now()] - The current timestamp.
*/
type Options = {
digits?: number
algorithm?: TOTPAlgorithm
encoding?: TOTPEncoding
period?: number
timestamp?: number
}
Expand All @@ -26,14 +29,17 @@ export class TOTP {
const _options: Required<Options> = {
digits: 6,
algorithm: "SHA-1",
encoding: "hex",
period: 30,
timestamp: Date.now(),
...options,
}
const epochSeconds = Math.floor(_options.timestamp / 1000)
const timeHex = this.dec2hex(Math.floor(epochSeconds / _options.period)).padStart(16, "0")

const hmacKey = await this.crypto.importKey("raw", this.base32ToBuffer(key), { name: "HMAC", hash: { name: _options.algorithm } }, false, ["sign"])
const keyBuffer = _options.encoding === "hex" ? this.base32ToBuffer(key) : this.asciiToBuffer(key)

const hmacKey = await this.crypto.importKey("raw", keyBuffer, { name: "HMAC", hash: { name: _options.algorithm } }, false, ["sign"])
const signature = await this.crypto.sign("HMAC", hmacKey, this.hex2buf(timeHex))

const signatureHex = this.buf2hex(signature)
Expand Down Expand Up @@ -91,6 +97,19 @@ export class TOTP {
return buffer.buffer as ArrayBuffer
}

/**
* Converts an ASCII string to an ArrayBuffer.
* @param {string} str - The ASCII string to convert.
* @returns {ArrayBuffer} The ArrayBuffer representation of the ASCII string.
*/
private static asciiToBuffer(str: string): ArrayBuffer {
const buffer = new Uint8Array(str.length)
for (let i = 0; i < str.length; i++) {
buffer[i] = str.charCodeAt(i)
}
return buffer.buffer as ArrayBuffer
}

/**
* Converts a hexadecimal string to an ArrayBuffer.
* @param {string} hex - The hexadecimal string to convert.
Expand Down
Loading