Code Monkey home page Code Monkey logo

dgoogauth's People

Contributors

dgryski avatar fmpwizard avatar g3kk0 avatar jpillora avatar kisom avatar mna avatar zemirco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dgoogauth's Issues

Spelling fix

In readme.md RTC 6238 should probably be RFC 6238.

Thanks for a very useful library.

Add LICENSE

I would like to include this library in one of my side projects but I don't want to violate your rights. Could you please update this repository with a LICENSE file so people understand the restrictions on its use?

See: https://opensource.org/licenses

Google has a very useful document which discusses its legal positions on each of the open source licenses as far as being consumed for commercial purposes. It's a good way to understand the consequences of certain licenses such as the WTFPL: https://opensource.google.com/docs/thirdparty/licenses/

ComputeCode sometimes returns to few digits

The following function sometimes returns 5 digits instead of 6:

dgoogauth/googauth.go

Lines 27 to 49 in 96977cb

func ComputeCode(secret string, value int64) int {
key, err := base32.StdEncoding.DecodeString(secret)
if err != nil {
return -1
}
hash := hmac.New(sha1.New, key)
err = binary.Write(hash, binary.BigEndian, value)
if err != nil {
return -1
}
h := hash.Sum(nil)
offset := h[19] & 0x0f
truncated := binary.BigEndian.Uint32(h[offset : offset+4])
truncated &= 0x7fffffff
code := truncated % 1000000
return int(code)
}

e.g.

package main

import (
  "crypto/hmac"
  "crypto/sha1"
  "encoding/base32"
  "encoding/binary"
  "fmt"
  "time"

  "github.com/dgryski/dgoogauth"
)

func main() {
  secret := []byte{'H', 'e', 'l', 'l', 'o', '!', 0xDE, 0xAD, 0xBE, 0xEF}
  secretEnc := base32.StdEncoding.EncodeToString(secret)

  var t0 int64
  t0 = int64(time.Now().Unix() / 2)
  code := dgoogauth.ComputeCode(secretEnc, t0)

  fmt.Printf("code = %+v\n", code)
}

func ComputeCode(secret string, value int64) int {
  key, err := base32.StdEncoding.DecodeString(secret)
  if err != nil {
    return -1
  }

  hash := hmac.New(sha1.New, key)
  err = binary.Write(hash, binary.BigEndian, value)
  if err != nil {
    return -1
  }
  h := hash.Sum(nil)

  offset := h[19] & 0x0f

  truncated := binary.BigEndian.Uint32(h[offset : offset+4])

  truncated &= 0x7fffffff
  code := truncated % 1000000

  return int(code)
}

Outputs the following when looped:

code = 292234
code = 108363
code = 108363
code = 828914
code = 45510
code = 45510
code = 45288
code = 45288
code = 286406
code = 166336
code = 166336
code = 569768
code = 569768

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.