{"id":20815190,"url":"https://github.com/hoishing/totp-auth","last_synced_at":"2025-05-07T12:10:22.089Z","repository":{"id":59321374,"uuid":"536635513","full_name":"hoishing/totp-auth","owner":"hoishing","description":"TOTP generator, compatible with Google Authenticator","archived":false,"fork":false,"pushed_at":"2023-02-09T04:53:20.000Z","size":57,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-19T18:05:09.904Z","etag":null,"topics":["2fa","authentication","javascript","npm-package","totp"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/totp-auth","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hoishing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-14T15:08:38.000Z","updated_at":"2024-08-06T11:44:32.000Z","dependencies_parsed_at":"2023-01-28T13:32:45.325Z","dependency_job_id":null,"html_url":"https://github.com/hoishing/totp-auth","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoishing%2Ftotp-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoishing%2Ftotp-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoishing%2Ftotp-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoishing%2Ftotp-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hoishing","download_url":"https://codeload.github.com/hoishing/totp-auth/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252873949,"owners_count":21817714,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2fa","authentication","javascript","npm-package","totp"],"created_at":"2024-11-17T21:19:55.571Z","updated_at":"2025-05-07T12:10:22.072Z","avatar_url":"https://github.com/hoishing.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TOTP Auth\n\n[![npm-badge]][npm-totp] ![ts-badge] ![prettier-badge] ![license-badge]\n\n\u003e Easy-to-use timebased one time password(TOTP) generator, compatible with Google Authenticator.\n\n🔗 [source code](https://github.com/hoishing/totp-auth)\n\n[npm-badge]: https://img.shields.io/npm/v/totp-auth\n[license-badge]: https://img.shields.io/npm/l/totp-auth\n[ts-badge]: https://img.shields.io/badge/%3C%2F%3E-TypeScript-blue\n[prettier-badge]: https://img.shields.io/badge/formatter-prettier-yellow\n\n## Installation\n\n```shell\n# pnpm\npnpm i totp-auth\n\n# npm\nnpm i totp-auth\n```\n\n## Usage\n\n```js\nimport { createTOTP, countdown } from \"totp-auth\"\nimport { setInterval } from \"timers/promises\"\n\n//secret from service provider\nconst secret = \"abcd1234\"\n\nlet totp = createTOTP(secret)\nlet expire = countdown()\n\n// current TOTP and expiring time in seconds\nconsole.log(`TOTP: ${totp}, expire: ${expire}`)\n\n// keep counting down and refresh TOTP every 30 sec\nfor await (let _ of setInterval(1000)) {\n  const cnt = countdown()\n  if (cnt \u003e= expire) totp = createTOTP(secret)\n  expire = cnt\n  console.log(`TOTP: ${totp}, expire: ${expire}`)\n}\n```\n\n### Error Handling\n\nNot all strings can be secret key, invalid secret key will return a customizable error message.\n\n```js\n// invalid secret -\u003e default error message\ncreateTOTP('asdf') // returns \"invalid secret\" \n\n// invalid secret w/ custom error message\ncreateTOTP('asdf', undefined, 'bad key') // returns \"bad key\" \n```\n\n## Technical Details\n\n📥 [npm package download][npm-totp]\n\n### code logic\n\n1. create base32 representation of the credential\n1. calculate HMAC hash from the credential with current time\n1. shift and trim 6 digit TOTP from the hash above\n\n### Tests\n\nBoth `createTOTP` and `countdown` are pure functions. Unit test with [Jest](https://jestjs.io/) are included.\n\nThe TOTP output could also simply verified by Google Authenticator output.\n\n## Credits\n\nAlgorithm ref: http://jsfiddle.net/russau/ch8PK/\nHMAC lib: https://github.com/Caligatio/jsSHA\n\n## FAQ\n\n- How can I extract secret keys from Google Authenticator?\n  - use another npm package: https://github.com/krissrex/google-authenticator-exporter\n  - use chrome extension: https://authenticator.cc/\n\n## Questions?\n\nOpen a [github issue] or ping me on [Twitter ![twitter-icon]][Twitter]\n\n[github issue]: https://github.com/hoishing/totp-auth/issues\n[Twitter]: https://twitter.com/intent/tweet?text=https://github.com/hoishing/totp-auth/%20%0D@hoishing\n[twitter-icon]: https://api.iconify.design/logos/twitter.svg?width=20\n[npm-totp]: https://www.npmjs.com/package/totp-auth\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoishing%2Ftotp-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoishing%2Ftotp-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoishing%2Ftotp-auth/lists"}