Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use it with go1.11 have a problem.twofactor.TOTPFromBytes(x,y) #22

Open
leon20181119 opened this issue Nov 30, 2018 · 2 comments
Open

Comments

@leon20181119
Copy link

No description provided.

@isuruceanu
Copy link

isuruceanu commented Nov 30, 2018

Have same issue

To reproduce the issue please use convert/bigendian/convert.go previous version

func TestBigEndianConvertion(t *testing.T) {
	totp, err := NewTOTP("[email protected]", "Sec51", crypto.SHA256, 6)
	if err != nil {
		t.Fatal(err)
	}

	data, err := totp.ToBytes()
	if err != nil {
		t.Fatal(err)
	}

	newTotp, err := TOTPFromBytes(data, "Sec51")
	if err != nil {
		t.Fatal(err)
	}

	if totp.account != newTotp.account {
		t.Error("Fail: Received totp do not match origin")
	}
}

Fails with:
panic: runtime error: slice bounds out of range [recovered]
panic: runtime error: slice bounds out of range

This is because reading issuerSize returns 0 although the byte array is ok.

The issue is in the implementation of

// helper function which converts a big endian []byte to a int
func FromInt(data [4]byte) int {
	i := (int(data[3]) << 0) | (int(data[2]) << 8) |
		(int(data[1]) << 16) | (int(data[0]) << 24)
	return int(i)
}

To Fix: just update latest version of convert library

The big question is why adding a Println of data in FromInt, makes the function to work fine

func FromInt(data [4]byte) int {
	fmt.Println(data)
	i := (int(data[3]) << 0) | (int(data[2]) << 8) |
		(int(data[1]) << 16) | (int(data[0]) << 24)
	return int(i)
}

isuruceanu pushed a commit to isuruceanu/twofactor that referenced this issue Nov 30, 2018
@leon20181119
Copy link
Author

thanks.now already solve.just update convert library.https://github.com/sec51/convert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants