Commit c71e7285 authored by Zhu Yong's avatar Zhu Yong
Browse files

start of Kinetic client library in Go

parents
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+1 −0
Original line number Diff line number Diff line
vendor/

Godeps/Godeps.json

0 → 100644
+11 −0
Original line number Diff line number Diff line
{
	"ImportPath": "github.com/yongzhy/kinetic-go",
	"GoVersion": "go1.6",
	"GodepVersion": "v74",
	"Deps": [
		{
			"ImportPath": "github.com/golang/protobuf/proto",
			"Rev": "888eb0692c857ec880338addf316bd662d5e630e"
		}
	]
}

Godeps/Readme

0 → 100644
+5 −0
Original line number Diff line number Diff line
This directory tree is generated automatically by godep.

Please do not edit.

See https://github.com/tools/godep for more information.

hmac.go

0 → 100644
+37 −0
Original line number Diff line number Diff line
package kinetic

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/binary"

	kproto "github.com/yongzhy/kinetic-go/proto"
)

func compute_hmac(data []byte, key []byte) []byte {
	mac := hmac.New(sha1.New, key)

	if data != nil && len(data) > 0 {
		ln := make([]byte, 4)
		binary.BigEndian.PutUint32(ln, uint32(len(data)))

		mac.Write(ln)
		mac.Write(data)
	}

	return mac.Sum(nil)
}

func validate_hmac(mesg *kproto.Message, key []byte) bool {
	if mesg != nil {
		real := compute_hmac(mesg.GetCommandBytes(), key)

		if mesg.GetHmacAuth() != nil {
			expect := mesg.GetHmacAuth().GetHmac()
			if hmac.Equal(real, expect) {
				return true
			}
		}
	}
	return false
}

hmac_test.go

0 → 100644
+34 −0
Original line number Diff line number Diff line
package kinetic

import (
	"bytes"
	"testing"

	proto "github.com/golang/protobuf/proto"
	kproto "github.com/yongzhy/kinetic-go/proto"
)

func TestHmacEmptyMessage(t *testing.T) {
	expected := []byte{0xa7, 0x7a, 0x6a, 0xda, 0x5c, 0xe6,
		0x7c, 0xf7, 0xae, 0xe4, 0x8a, 0x79, 0xd4, 0x86,
		0x6b, 0xb2, 0x71, 0x24, 0x18, 0x15}
	hmac := compute_hmac(nil, []byte("asdfasdf"))

	if !bytes.Equal(expected, hmac) {
		t.Fatal("HMAC for empty Command Failed")
	}
}

func TestHmacSimpleMessage(t *testing.T) {
	expected := []byte{0x40, 0x5F, 0x94, 0x9F, 0xC3, 0x50,
		0xDC, 0x0B, 0x6A, 0x5A, 0x9D, 0x27, 0xA3, 0xCA,
		0x44, 0x58, 0x9D, 0xB3, 0x4A, 0xCD}
	cmd := kproto.Command{nil, nil, nil, nil}
	code := kproto.Command_Status_SUCCESS
	cmd.Status = &kproto.Command_Status{&code, nil, nil, nil}
	cmdBytes, _ := proto.Marshal(&cmd)
	hmac := compute_hmac(cmdBytes, []byte("asdfasdf"))
	if !bytes.Equal(expected, hmac) {
		t.Fatal("HMAC for simple Command Failed")
	}
}