Git Product home page Git Product logo

mruby-otp's Introduction

mruby-otp CircleCI Actions Status: CI GitHub version

A library to generate and verify OTPs (HOTP & TOTP) according to RFC 4226 and RFC 6238.

Installation

Add the usual conf.gem line to build_config.rb:

MRuby::Build.new do |conf|
  # ...

  conf.gem :github => 'baldowl/mruby-otp'
end

Examples

> hotp = HOTP.new(Base32.encode('12345678901234567890'))
 => #<HOTP:0x7fa74a0049b0 @digits=6, @digest="sha1", @type="hotp", @secret="12345678901234567890">
> p hotp.at(0)
"755224"
 => "755224"
> p hotp.at(1)
"287082"
 => "287082"
> p hotp.verify("755224")
true
 => true
> p hotp.verify("287082", :at => 1)
true
 => true
> p hotp.verify("287082", :at => 2)
false
 => false
> p hotp.uri("J. Doe", :issuer => "ExampleNet Inc")
"otpauth://hotp/ExampleNet%20Inc%3AJ.%20Doe?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&digits=6&counter=&issuer=ExampleNet%20Inc"
 => "otpauth://hotp/ExampleNet%20Inc%3AJ.%20Doe?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&digits=6&counter=&issuer=ExampleNet%20Inc"
> totp = TOTP.new(Base32.encode('12345678901234567890'))
 => #<TOTP:0x7fa38681cfb0 @digits=6, @digest="sha1", @type="totp", @interval=30, @secret="12345678901234567890">
> p totp.at(Time.now)
"829461"
 => "829461"
> p totp.current
"829461"
 => "829461"
> t = Time.gm(1997, 8, 29, 5, 14, 00)
 => Fri Aug 29 05:14:00 UTC 1997
> p totp.at(t)
"281836"
 => "281836"
> p totp.verify(281836, :at => t)
true
 => true
> t2 = Time.gm(1997, 8, 29, 5, 14, 45)
 => Fri Aug 29 05:14:45 UTC 1997
> p totp.verify(281836, :at => t2)
false
 => false
> p totp.verify(281836, :at => t2, :drift => 25)
true
 => true
> p totp.uri("J. Doe", :issuer => "ExampleNet Inc")
"otpauth://totp/ExampleNet%20Inc%3AJ.%20Doe?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&digits=6&period=30&issuer=ExampleNet%20Inc"
 => "otpauth://totp/ExampleNet%20Inc%3AJ.%20Doe?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&digits=6&period=30&issuer=ExampleNet%20Inc"
> 

License

This code is released under the MIT License: see LICENSE file.

mruby-otp's People

Contributors

baldowl avatar dependabot[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mikehale

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.