Пример #1
0
/*
	Test base64url.Encode function using a hex encoded string corresponding to an Base64URLEncoded string.
	First we decode the hex encoded bytes and then run the encode function on this and check the result with the
	corresponding Base64url encoded data.
*/
func TestEncode(t *testing.T) {
	testBytes := make([]byte, len(HEXEncoded)/2)
	hex.Decode(testBytes, []byte(HEXEncoded))
	resultStr := base64url.Encode(testBytes)
	if Base64URLEncoded != resultStr {
		t.Errorf(Base64URLEncoded + " not equal with " + resultStr)
	}
}
Пример #2
0
/*
	Generate a 32 bytes (128 bits) nonce from ipv4 address, timestamp, counter and random as
	suggested in SQRL documentation (https://www.grc.com/sqrl/server.htm)
	Encode this with AES key generated at server start and return
*/
func (r *Nonce) Generate(remoteAddr string) string {
	/*
		Prepare nut (128 bits)
	*/
	nut := make([]byte, 16)

	/*
		Prepare ipv4 address (32 bits)
	*/
	if strings.Contains(remoteAddr, "]") {
		remoteAddr = strings.Split(remoteAddr, "]")[0]
		remoteAddr = strings.Replace(remoteAddr, "[", "", -1)
	} else {
		remoteAddr = strings.Split(remoteAddr, ":")[0]
	}
	ipAddr := []byte(net.ParseIP(remoteAddr))
	ipv4Addr := ipAddr[len(ipAddr)-4:]
	copy(nut[0:4], ipv4Addr)

	/*
		Prepare unix timestamp (32 bits)
	*/
	binary.LittleEndian.PutUint32(nut[4:8], uint32(time.Now().Unix()))

	/*
		Prepare a global counter (32 bits)
	*/
	r.global_counter++
	binary.LittleEndian.PutUint32(nut[8:12], r.global_counter)

	/*
		Prepare a random uint32 (32 bits)
	*/
	rand.Seed(time.Now().Unix())
	binary.LittleEndian.PutUint32(nut[12:16], rand.Uint32())

	/*
		Encrypt nut with AES key and return base64url encoded string.
	*/
	encryptedNut := make([]byte, 16)
	r.aesKeyBlock.Encrypt(encryptedNut, nut)
	return base64url.Encode(encryptedNut)
}