twofactor icon indicating copy to clipboard operation
twofactor copied to clipboard

Keep getting "Tokens mismatch" error

Open IvRRimum opened this issue 5 years ago • 3 comments

So, yeah. Validate returns Tokens mismatch. I can't seem to understand why ://

package main

import (
	//	"crypto"
	b64 "encoding/base64"
	"fmt"
	"github.com/sec51/twofactor"
)

func main() {
	// otpauth://totp/Cryptoapl:[email protected]?secret=JEAM3MSPDPI5TLWDM4DEE4OCYDRVPSXY&issuer=Cryptoapl:karlis
	issuer := "somesite"
	//	otp, err := twofactor.NewTOTP("[email protected]", issuer, crypto.SHA1, 8)
	//	if err != nil {
	//		fmt.Println(err)
	//		return
	//	}
	//
	//	bytes, err := otp.ToBytes()
	//	if err != nil {
	//		fmt.Println(err)
	//		return
	//	}

	//	bytesToString := string(bytes)
	//	base64EncodedBytes := b64.StdEncoding.EncodeToString([]byte(bytesToString))
	//	fmt.Println(base64EncodedBytes)

	stringOfBytes, _ := b64.StdEncoding.DecodeString("oQAAAAAAAACcALyxTRxmg1YFxKYPcgR4s/e+K/mqlC6M92BekB51Wor/tdD1Q3/pj2RxUmpNzU79P65u2Oefj+lPgHBixKvWEWwLgH22nM/zo9SCf5umOn2txUrsfJPPzQSmG1SO3HFoSFkKvMMR7brDuHn53bDTS1l5+VeoR/QGrugglt8w8jcOOaDHGxahaZM9LmhBBlubT+P7cP5ndUA=")
	stringToBytes := []byte(stringOfBytes)

	otpTwo, err := twofactor.TOTPFromBytes(stringToBytes, issuer)
	if err != nil {
		fmt.Println(err)
		return
	}

	//	fmt.Println(otp.Secret())
	fmt.Println(otpTwo.Secret())

	authCode := "911849"
	err = otpTwo.Validate(authCode)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("Successful Authorisation!")
	return

}

IvRRimum avatar Dec 14 '18 13:12 IvRRimum

I have the same Tokens mismatch issue but your code doesn't seem right to me. Why is authCode hard coded? you should take it out of your authenticator app, not choose randomly

chiptus avatar Dec 31 '18 14:12 chiptus

I have the same Tokens mismatch issue when I choose crypto.SHA256. When I change back to crypto.SHA1 it works fine.

xiaoxiaff avatar Apr 26 '19 23:04 xiaoxiaff

FreeOTP supports crypto.SHA1, crypto.SHA256 and crypto.SHA512.

Authy, Google authenticator, Microsoft authenticator, Okta support only crypto.SHA1.

So, better to use only crypto.SHA1 for now...

pilinux avatar Aug 04 '22 10:08 pilinux