{"id":13636480,"url":"https://github.com/constantoine/totp-rs","last_synced_at":"2025-05-16T01:06:12.156Z","repository":{"id":37821309,"uuid":"254939322","full_name":"constantoine/totp-rs","owner":"constantoine","description":"RFC-compliant TOTP implementation with ease of use as a goal and additionnal QoL features.","archived":false,"fork":false,"pushed_at":"2025-04-12T08:27:23.000Z","size":311,"stargazers_count":205,"open_issues_count":2,"forks_count":23,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T03:10:55.153Z","etag":null,"topics":["2fa","rust","totp","web"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/totp-rs","language":"Rust","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/constantoine.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-04-11T19:24:14.000Z","updated_at":"2025-04-18T06:02:04.000Z","dependencies_parsed_at":"2025-04-11T23:29:32.301Z","dependency_job_id":"fcc68a3c-cb7e-4151-9bb3-977a7dbfee66","html_url":"https://github.com/constantoine/totp-rs","commit_stats":{"total_commits":152,"total_committers":13,"mean_commits":"11.692307692307692","dds":0.5789473684210527,"last_synced_commit":"81c773f471f7c2909f414662aff02cdc416552ab"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/constantoine%2Ftotp-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/constantoine%2Ftotp-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/constantoine%2Ftotp-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/constantoine%2Ftotp-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/constantoine","download_url":"https://codeload.github.com/constantoine/totp-rs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254448579,"owners_count":22072764,"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","rust","totp","web"],"created_at":"2024-08-02T00:01:01.785Z","updated_at":"2025-05-16T01:06:07.132Z","avatar_url":"https://github.com/constantoine.png","language":"Rust","funding_links":[],"categories":["库 Libraries","Libraries","Rust"],"sub_categories":["身份验证 Authentication","Authentication"],"readme":"# totp-rs\n![Build Status](https://github.com/constantoine/totp-rs/workflows/Rust/badge.svg) [![docs](https://docs.rs/totp-rs/badge.svg)](https://docs.rs/totp-rs) [![](https://img.shields.io/crates/v/totp-rs.svg)](https://crates.io/crates/totp-rs) [![codecov](https://codecov.io/gh/constantoine/totp-rs/branch/master/graph/badge.svg?token=Q50RAIFVWZ)](https://codecov.io/gh/constantoine/totp-rs) [![cargo-audit](https://github.com/constantoine/totp-rs/actions/workflows/security.yml/badge.svg)](https://github.com/constantoine/totp-rs/actions/workflows/security.yml)\n\nThis library permits the creation of 2FA authentification tokens per TOTP, the verification of said tokens, with configurable time skew, validity time of each token, algorithm and number of digits! Default features are kept as lightweight as possible to ensure small binaries and short compilation time.\n\nIt now supports parsing [otpauth URLs](https://github.com/google/google-authenticator/wiki/Key-Uri-Format) into a totp object, with sane default values.\n\nBe aware that some authenticator apps will accept the `SHA256` and `SHA512` algorithms but silently fallback to `SHA1` which will make the `check()` function fail due to mismatched algorithms.\n\n## Features\n---\n### qr\nWith optional feature \"qr\", you can use it to generate a base64 png qrcode. This will enable feature `otpauth`.\n### otpauth\nWith optional feature \"otpauth\", support parsing the TOTP parameters from an `otpauth` URL, and generating an `otpauth` URL. It adds 2 fields to `TOTP`.\n### serde_support\nWith optional feature \"serde_support\", library-defined types `TOTP` and `Algorithm` and will be Deserialize-able and Serialize-able.\n### gen_secret\nWith optional feature \"gen_secret\", a secret will be generated for you to store in database.\n### zeroize\nSecurely zero secret information when the TOTP struct is dropped.\n### steam\nAdd support for Steam TOTP tokens.\n\n\n# Examples\n\n## Summary\n\n0. [Understanding Secret](#understanding-secret)\n1. [Generate a token](#generate-a-token)\n2. [Enable qrcode generation](#with-qrcode-generation)\n3. [Enable serde support](#with-serde-support)\n4. [Enable otpauth url support](#with-otpauth-url-support)\n5. [Enable gen_secret support](#with-gensecret)\n6. [With RFC-6238 compliant default](#with-rfc-6238-compliant-default)\n7. [New TOTP from steam secret](#new-totp-from-steam-secret)\n\n### Understanding Secret\n---\nThis new type was added as a disambiguation between Raw and already base32 encoded secrets.\n```Rust\nSecret::Raw(\"TestSecretSuperSecret\".as_bytes().to_vec())\n```\nIs equivalent to\n```Rust\nSecret::Encoded(\"KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ\".to_string())\n```\n### Generate a token\n---\nAdd it to your `Cargo.toml`:\n```toml\n[dependencies]\ntotp-rs = \"^5.0\"\n```\nYou can then do something like:\n```Rust\nuse std::time::SystemTime;\nuse totp_rs::{Algorithm, TOTP, Secret};\n\nfn main() {\n    let totp = TOTP::new(\n        Algorithm::SHA1,\n        6,\n        1,\n        30,\n        Secret::Raw(\"TestSecretSuperSecret\".as_bytes().to_vec()).to_bytes().unwrap(),\n    ).unwrap();\n    let token = totp.generate_current().unwrap();\n    println!(\"{}\", token);   \n}\n```\nWhich is equivalent to:\n```Rust\nuse std::time::SystemTime;\nuse totp_rs::{Algorithm, TOTP, Secret};\n\nfn main() {\n    let totp = TOTP::new(\n        Algorithm::SHA1,\n        6,\n        1,\n        30,\n        Secret::Encoded(\"KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ\".to_string()).to_bytes().unwrap(),\n    ).unwrap();\n    let token = totp.generate_current().unwrap();\n    println!(\"{}\", token);   \n}\n```\n### With qrcode generation\n---\nAdd it to your `Cargo.toml`:\n```toml\n[dependencies.totp-rs]\nversion = \"^5.3\"\nfeatures = [\"qr\"]\n```\nYou can then do something like:\n```Rust\nuse totp_rs::{Algorithm, TOTP, Secret};\n\nfn main() {\n    let totp = TOTP::new(\n        Algorithm::SHA1,\n        6,\n        1,\n        30,\n        Secret::Encoded(\"KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ\".to_string()).to_bytes().unwrap(),\n        Some(\"Github\".to_string()),\n        \"constantoine@github.com\".to_string(),\n    ).unwrap();\n    let qr_code = totp.get_qr_base64()?;\n    println!(\"{}\", qr_code);   \n}\n```\n\n### With serde support\n---\nAdd it to your `Cargo.toml`:\n```toml\n[dependencies.totp-rs]\nversion = \"^5.0\"\nfeatures = [\"serde_support\"]\n```\n\n### With otpauth url support\n---\nAdd it to your `Cargo.toml`:\n```toml\n[dependencies.totp-rs]\nversion = \"^5.0\"\nfeatures = [\"otpauth\"]\n```\nYou can then do something like:\n```Rust\nuse totp_rs::TOTP;\n\nfn main() {\n    let otpauth = \"otpauth://totp/GitHub:constantoine@github.com?secret=KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ\u0026issuer=GitHub\";\n    let totp = TOTP::from_url(otpauth).unwrap();\n    println!(\"{}\", totp.generate_current().unwrap());\n}\n```\n\n### With gen_secret\n---\nAdd it to your `Cargo.toml`:\n```toml\n[dependencies.totp-rs]\nversion = \"^5.3\"\nfeatures = [\"gen_secret\"]\n```\nYou can then do something like:\n```Rust\nuse totp_rs::{Algorithm, TOTP, Secret};\n\nfn main() {\n    let totp = TOTP::new(\n        Algorithm::SHA1,\n        6,\n        1,\n        30,\n        Secret::default().to_bytes().unwrap(),\n        Some(\"Github\".to_string()),\n        \"constantoine@github.com\".to_string(),\n    ).unwrap();\n    let qr_code = totp.get_qr_base64()?;\n    println!(\"{}\", qr_code);   \n}\n```\nWhich is equivalent to\n```Rust\nuse totp_rs::{Algorithm, TOTP, Secret};\n\nfn main() {\n    let totp = TOTP::new(\n        Algorithm::SHA1,\n        6,\n        1,\n        30,\n        Secret::generate_secret().to_bytes().unwrap(),\n        Some(\"Github\".to_string()),\n        \"constantoine@github.com\".to_string(),\n    ).unwrap();\n    let qr_code = totp.get_qr_base64()?;\n    println!(\"{}\", qr_code);   \n}\n```\n\n### With RFC-6238 compliant default\n---\nYou can do something like this\n```Rust\nuse totp_rs::{Algorithm, TOTP, Secret, Rfc6238};\n\nfn main () {\n    let mut rfc = Rfc6238::with_defaults(\n            Secret::Encoded(\"KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ\".to_string()).to_bytes().unwrap(),\n        )\n        .unwrap();\n\n    // optional, set digits\n    rfc.digits(8).unwrap();\n\n    // create a TOTP from rfc\n    let totp = TOTP::from_rfc6238(rfc).unwrap();\n    let code = totp.generate_current().unwrap();\n    println!(\"code: {}\", code);\n}\n```\nWith `gen_secret` feature, you can go even further and have all values by default and a secure secret.\n\nNote: With `otpauth` feature, `TOTP.issuer` will be `None`, and `TOTP.account_name` will be `\"\"`. Be sure to set those fields before generating an URL/QRCode\n```Rust\nfn main() {\n    let totp = TOTP::default();\n    let code = totp.generate_current().unwrap();\n    println!(\"code: {}\", code);\n}\n```\n\n### New TOTP from steam secret\n---\nAdd it to your `Cargo.toml`:\n```toml\n[dependencies.totp-rs]\nversion = \"^5.3\"\nfeatures = [\"steam\"]\n```\nYou can then do something like:\n```Rust\nuse totp_rs::{TOTP, Secret};\n\nfn main() {\n    let totp = TOTP::new_steam(\n        Secret::Encoded(\"KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ\".to_string()).to_bytes().unwrap(),\n    ).unwrap();\n    let code = totp.generate_current().unwrap();\n    println!(\"code: {}\", code);   \n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconstantoine%2Ftotp-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconstantoine%2Ftotp-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconstantoine%2Ftotp-rs/lists"}