forked from all_user/stat_center
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package googauth
|
|
|
|
import (
|
|
"encoding/base32"
|
|
"fmt"
|
|
"stat/common"
|
|
"testing"
|
|
|
|
"github.com/dgryski/dgoogauth"
|
|
)
|
|
|
|
func TestHotp(t *testing.T) {
|
|
secret := base32.StdEncoding.EncodeToString([]byte(fmt.Sprintf("admin%s", common.RandStr(10))))
|
|
fmt.Printf(secret)
|
|
//d := int64(rand.Intn(100000))
|
|
//randstr := strconv.FormatInt(d, 10)
|
|
//ra := strconv.FormatInt(120868, 10)
|
|
////serc := NewHotp("MFSG22LOORUXAVTINJRFGZ2Q", randstr)
|
|
//res, _ := VerifyHotp("MFSG22LOORUXAVTINJRFGZ2Q", ra)
|
|
////fmt.Println(err)
|
|
////fmt.Println(res)
|
|
////fmt.Println(serc)
|
|
//fmt.Println(res)
|
|
//h := NewHOTP([]byte("abcdefghijk"), 6)
|
|
//fmt.Println(h.GenerateCode())
|
|
//fmt.Println(h)
|
|
}
|
|
|
|
// func TestRandBase(t *testing.T) {
|
|
|
|
// for i := 0; i < 10; i++ {
|
|
// tn := common.RandIntBase(1564, 1, 5000)
|
|
// fmt.Println(tn)
|
|
// h := NewHOTP([]byte("abcdefghijk"), 5)
|
|
// code := h.GenerateCode(uint64(tn))
|
|
// res := h.VerifyCode(code, uint64(tn))
|
|
// fmt.Println(res)
|
|
// }
|
|
// }
|
|
|
|
//func TestCode(t *testing.T) {
|
|
// for i := 0; i < 10; i++ {
|
|
// count := common.RandIntBase(12345678, 1, 99999999)
|
|
// fmt.Println(count)
|
|
// dd := dgoogauth.ComputeCode("MRSHM6LTMFYHAMJSGM2DKNQ=", 1234)
|
|
// authCode := strconv.FormatInt(int64(dd), 10)
|
|
// re, _ := VerifyHotp("MRSHM6LTMFYHAMJSGM2DKNQ=", authCode, 1234)
|
|
// fmt.Println(re)
|
|
// }
|
|
//}
|
|
|
|
func TestHotpCode(t *testing.T) {
|
|
|
|
var HotpCfg = dgoogauth.OTPConfig{
|
|
Secret: "MRSHM6LTMFYHAMJSGM2DKNQ=",
|
|
HotpCounter: 1,
|
|
WindowSize: 3,
|
|
}
|
|
|
|
var cotp dgoogauth.OTPConfig
|
|
|
|
// reuse our test values from above
|
|
// perhaps create more?
|
|
cotp.Secret = "2SH3V3GDW7ZNMGYE"
|
|
cotp.HotpCounter = 1
|
|
cotp.WindowSize = 10
|
|
|
|
//var counterCodes = []struct {
|
|
// code int
|
|
// result bool
|
|
// counter int
|
|
//}{
|
|
// { /* 1 */ 293240, true, 2}, // increments on success
|
|
// { /* 1 */ 293240, false, 3}, // and failure
|
|
// { /* 5 */ 932068, true, 6}, // inside of window
|
|
// { /* 10 */ 481725, false, 7}, // outside of window
|
|
// { /* 10 */ 481725, false, 8}, // outside of window
|
|
// { /* 10 */ 481725, true, 11}, // now inside of window
|
|
//}
|
|
|
|
//for _, s := range counterCodes {
|
|
// authCode := strconv.FormatInt(int64(s.code), 10)
|
|
// res, _ := cotp.Authenticate(authCode)
|
|
// fmt.Println(res)
|
|
//}
|
|
for i := 0; i < 5; i++ {
|
|
res, _ := VerifyHtop(&HotpCfg, "807708", 322904702)
|
|
//res2, _ := cotp.Authenticate("293240")
|
|
fmt.Println(res)
|
|
//fmt.Println(res2)
|
|
//fmt.Println(cotp)
|
|
}
|
|
}
|