{"id":18015216,"url":"https://github.com/jchambers/java-otp","last_synced_at":"2025-04-13T00:47:21.999Z","repository":{"id":10248063,"uuid":"65097874","full_name":"jchambers/java-otp","owner":"jchambers","description":"A one-time password (HOTP/TOTP) library for Java","archived":false,"fork":false,"pushed_at":"2022-07-31T16:51:41.000Z","size":243,"stargazers_count":466,"open_issues_count":0,"forks_count":123,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-04-13T00:47:18.560Z","etag":null,"topics":["2fa","hotp","java","one-time-password","otp","security","totp","two-factor-authentication"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jchambers.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":"2016-08-06T19:07:44.000Z","updated_at":"2025-04-11T02:03:35.000Z","dependencies_parsed_at":"2022-07-30T21:08:09.881Z","dependency_job_id":null,"html_url":"https://github.com/jchambers/java-otp","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchambers%2Fjava-otp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchambers%2Fjava-otp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchambers%2Fjava-otp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchambers%2Fjava-otp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jchambers","download_url":"https://codeload.github.com/jchambers/java-otp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650434,"owners_count":21139672,"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","hotp","java","one-time-password","otp","security","totp","two-factor-authentication"],"created_at":"2024-10-30T04:12:56.271Z","updated_at":"2025-04-13T00:47:21.973Z","avatar_url":"https://github.com/jchambers.png","language":"Java","readme":"[![Build status](https://travis-ci.org/jchambers/java-otp.svg?branch=master)](https://travis-ci.org/jchambers/java-otp)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.eatthepath/java-otp/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.eatthepath/java-otp)\n\njava-otp is a Java library for generating [HOTP (RFC 4226)](https://tools.ietf.org/html/rfc4226) or [TOTP (RFC 6238)](https://tools.ietf.org/html/rfc6238) one-time passwords.\n\n## Getting java-otp\n\nYou can download java-otp as a jar file (it has no dependencies) from the [GitHub releases page](https://github.com/jchambers/java-otp/releases) and add it to your project's classpath. If you're using Maven (or something that understands Maven dependencies) to build your project, you can add java-otp as a dependency:\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.eatthepath\u003c/groupId\u003e\n  \u003cartifactId\u003ejava-otp\u003c/artifactId\u003e\n  \u003cversion\u003e0.4.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\njava-otp works with Java 8 or newer. If you need support for versions of Java older than Java 8, you may try using [java-otp v0.1](https://github.com/jchambers/java-otp/releases/tag/java-otp-0.1.0) (although it is no longer supported).\n\n## Documentation\n\nThe latest API docs are available at https://jchambers.github.io/java-otp/apidocs/latest/.\n\n## Usage\n\nTo demonstrate generating one-time passwords, we'll focus on the TOTP algorithm. To create a TOTP generator with a default password length (6 digits), time step (30 seconds), and HMAC algorithm (HMAC-SHA1):\n\n```java\nfinal TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator();\n```\n\nTo actually generate time-based one-time passwords, you'll need a key and a timestamp. Secure key management is beyond the scope of this document; for the purposes of an example, though, we'll generate a random key:\n\n```java\nfinal Key key;\n{\n    final KeyGenerator keyGenerator = KeyGenerator.getInstance(totp.getAlgorithm());\n\n    // Key length should match the length of the HMAC output (160 bits for SHA-1, 256 bits\n    // for SHA-256, and 512 bits for SHA-512). Note that while Mac#getMacLength() returns a\n    // length in _bytes,_ KeyGenerator#init(int) takes a key length in _bits._\n    final int macLengthInBytes = Mac.getInstance(totp.getAlgorithm()).getMacLength();\n    keyGenerator.init(macLengthInBytes * 8);\n\n    key = keyGenerator.generateKey();\n}\n```\n\nArmed with a key, we can deterministically generate one-time passwords for any timestamp:\n\n```java\nfinal Instant now = Instant.now();\nfinal Instant later = now.plus(totp.getTimeStep());\n\nSystem.out.println(\"Current password: \" + totp.generateOneTimePasswordString(key, now));\nSystem.out.println(\"Future password:  \" + totp.generateOneTimePasswordString(key, later));\n```\n\n…which produces (for one randomly-generated key):\n\n```\nCurrent password: 164092\nFuture password:  046148\n```\n\n## Performance and best practices\n\nOne-time password generators are thread-safe and reusable. Generally, applications should treat one-time password generator instances as long-lived resources (as opposed to creating new generators for each password-generation call).\n\n## License and copyright\n\njava-otp is published under the [MIT License](https://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["安全"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjchambers%2Fjava-otp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjchambers%2Fjava-otp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjchambers%2Fjava-otp/lists"}