Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from zhevron/new-repo
Browse files Browse the repository at this point in the history
New repository name
  • Loading branch information
zhevron committed Feb 17, 2015
2 parents b0e723b + 47bff73 commit 8b6a87a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 37 deletions.
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
go2fa - Two-factor authentication for Google Go
===============================================
twofactor - Two-factor authentication library
=============================================

[![Coverage Status](https://img.shields.io/coveralls/zhevron/go2fa.svg)](https://coveralls.io/r/zhevron/go2fa)
[![Build Status](https://travis-ci.org/zhevron/go2fa.svg?branch=master)](https://travis-ci.org/zhevron/go2fa)
[![GoDoc](https://godoc.org/github.com/zhevron/go2fa?status.svg)](https://godoc.org/github.com/zhevron/go2fa)
[![Coverage Status](https://img.shields.io/coveralls/zhevron/twofactor.svg)](https://coveralls.io/r/zhevron/twofactor)
[![Build Status](https://travis-ci.org/zhevron/twofactor.svg?branch=master)](https://travis-ci.org/zhevron/twofactor)

go2fa is a Go library for handling two-factor authentication.
**twofactor** is a library for handling two-factor authentication in [Go](https://golang.org/).
To use the library, you need to install one of the implementations below.

You can install the library using Go:
## YAML

```
go get gopkg.in/zhevron/go2fa.v1
```
[![GoDoc](https://godoc.org/github.com/zhevron/twofactor/totp?status.svg)](https://godoc.org/github.com/zhevron/twofactor/totp)

You can also choose to install one of the implementations instead.
This implementation reads and writes [YAML](http://www.yaml.org/) files.

Time-based One Time Password (TOTP) [![GoDoc](https://godoc.org/github.com/zhevron/go2fa/totp?status.svg)](https://godoc.org/github.com/zhevron/go2fa/totp)
```
go get gopkg.in/zhevron/go2fa.v1/totp
go get gopkg.in/zhevron/twofactor.v1/totp
```

go2fa is licensed under the [MIT license](http://opensource.org/licenses/MIT).
## License

**twofactor** is licensed under the [MIT license](http://opensource.org/licenses/MIT).
16 changes: 8 additions & 8 deletions totp/totp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//
// To be able to generate codes, you need to create a TOTP object using a secret:
// import (
// "gopkg.in/zhevron/go2fa.v1"
// "gopkg.in/zhevron/go2fa.v1/totp"
// "gopkg.in/zhevron/twofactor.v1"
// "gopkg.in/zhevron/twofactor.v1/totp"
// )
// secret := go2fa.NewSecret(0)
// secret := twofactor.NewSecret(0)
// otp := totp.NewTOTP(secret, 0, 0)
//
// After creating the TOTP object, you can easily generate the current code by calling the Code method:
Expand All @@ -33,20 +33,20 @@ import (
"math"
"time"

"github.com/zhevron/go2fa"
"github.com/zhevron/twofactor"
)

// TOTP implements time-based one time passwords as specified in RFC6238 (http://tools.ietf.org/html/rfc6238).
type TOTP struct {
secret go2fa.Secret
secret twofactor.Secret
length int
duration int
}

// NewTOTP makes a new TOTP object for generating OTP codes from a given Secret.
// If length is 0 or less, it will default to a length of 6.
// If duration is 0 or less, it will default to a duration of 30 seconds.
func NewTOTP(s go2fa.Secret, length int, duration int) *TOTP {
func NewTOTP(s twofactor.Secret, length int, duration int) *TOTP {
if length <= 0 {
length = 6
}
Expand All @@ -66,7 +66,7 @@ func (t TOTP) Code() (int32, error) {
}

// Secret returns the secret for this TOTP object.
func (t TOTP) Secret() go2fa.Secret {
func (t TOTP) Secret() twofactor.Secret {
return t.secret
}

Expand Down Expand Up @@ -107,7 +107,7 @@ func (t TOTP) generateCode(n int64) (int32, error) {
b, err := t.secret.Bytes()
if err == nil {
h := hmac.New(sha1.New, b)
h.Write(go2fa.Int64ToBytes(n))
h.Write(twofactor.Int64ToBytes(n))
hash := h.Sum(nil)
offset := int((len(hash) - 1) & 0xf)
binary := int32(hash[offset]&0x7f)<<24 |
Expand Down
6 changes: 3 additions & 3 deletions totp/totp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package totp
import (
"testing"

"github.com/zhevron/go2fa"
"github.com/zhevron/twofactor"
. "gopkg.in/check.v1"
)

Expand All @@ -12,14 +12,14 @@ func Test(t *testing.T) {
}

type TOTPSuite struct {
secret go2fa.Secret
secret twofactor.Secret
totp *TOTP
}

var _ = Suite(&TOTPSuite{})

func (s *TOTPSuite) SetUpTest(c *C) {
s.secret, _ = go2fa.NewSecret(0)
s.secret, _ = twofactor.NewSecret(0)
s.totp = NewTOTP(s.secret, 0, 0)
}

Expand Down
8 changes: 4 additions & 4 deletions go2fa.go → twofactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// Use of this source code is governed by the MIT license.
// See LICENSE.md for details.

// Package go2fa contains support functions and types for two-factor authentication.
// Package twofactor contains support functions and types for two-factor authentication.
//
// This package provides shared structs and functions for all implementations.
//
// To generate a new secret for 2FA, use the NewSecret method:
// import "gopkg.in/zhevron/go2fa.v1"
// secret := go2fa.NewSecret(0)
package go2fa
// import "gopkg.in/zhevron/twofactor.v1"
// secret := twofactor.NewSecret(0)
package twofactor

import (
"crypto/rand"
Expand Down
16 changes: 8 additions & 8 deletions go2fa_test.go → twofactor_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package go2fa
package twofactor

import (
"bytes"
Expand All @@ -12,11 +12,11 @@ func Test(t *testing.T) {
TestingT(t)
}

type Go2faSuite struct{}
type TwoFactorSuite struct{}

var _ = Suite(&Go2faSuite{})
var _ = Suite(&TwoFactorSuite{})

func (s *Go2faSuite) TestInt64ToBytes(c *C) {
func (s *TwoFactorSuite) TestInt64ToBytes(c *C) {
ints := []int64{10, 200, 3000, 40000, 500000}
intBytes := [][]byte{
[]byte{0, 0, 0, 0, 0, 0, 0, 10},
Expand All @@ -30,12 +30,12 @@ func (s *Go2faSuite) TestInt64ToBytes(c *C) {
}
}

func (s *Go2faSuite) TestNewSecret(c *C) {
func (s *TwoFactorSuite) TestNewSecret(c *C) {
secret, _ := NewSecret(0)
c.Check(len(secret), Equals, 16)
}

func (s *Go2faSuite) TestSecretBytes(c *C) {
func (s *TwoFactorSuite) TestSecretBytes(c *C) {
str := "thisisasecret"
strb, _ := base32.StdEncoding.DecodeString(str)
secret := Secret(str)
Expand All @@ -46,13 +46,13 @@ func (s *Go2faSuite) TestSecretBytes(c *C) {
}
}

func (s *Go2faSuite) TestSecretString(c *C) {
func (s *TwoFactorSuite) TestSecretString(c *C) {
str := "thisisasecret"
secret := Secret(str)
c.Check(secret.String(), Equals, str)
}

func (s *Go2faSuite) BenchmarkNewSecret(c *C) {
func (s *TwoFactorSuite) BenchmarkNewSecret(c *C) {
for i := 0; i < c.N; i++ {
NewSecret(0)
}
Expand Down

0 comments on commit 8b6a87a

Please sign in to comment.