diff --git a/bitfinexhttp.go b/bitfinexhttp.go index cc90dc24..df855040 100644 --- a/bitfinexhttp.go +++ b/bitfinexhttp.go @@ -4,7 +4,6 @@ import ( "fmt" "log" "encoding/json" - "crypto/sha512" "errors" "strings" "strconv" @@ -257,7 +256,7 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequest(method, path string, params map[ } PayloadBase64 := Base64Encode(PayloadJson) - hmac := GetHMAC(sha512.New384, []byte(PayloadBase64), []byte(b.APISecret)) + hmac := GetHMAC(HASH_SHA512_384, []byte(PayloadBase64), []byte(b.APISecret)) headers := make(map[string]string) headers["X-BFX-APIKEY"] = b.APIKey headers["X-BFX-PAYLOAD"] = PayloadBase64 diff --git a/bitstamphttp.go b/bitstamphttp.go index 682ec1dc..e94264d7 100644 --- a/bitstamphttp.go +++ b/bitstamphttp.go @@ -4,7 +4,6 @@ import ( "net/url" "log" "encoding/json" - "crypto/sha256" "strings" "strconv" "errors" @@ -268,7 +267,7 @@ func (b *Bitstamp) SendAuthenticatedHTTPRequest(path string, values url.Values, nonce := strconv.FormatInt(time.Now().UnixNano(), 10) values.Set("key", b.APIKey) values.Set("nonce", nonce) - hmac := GetHMAC(sha256.New, []byte(nonce + b.ClientID + b.APIKey), []byte(b.APISecret)) + hmac := GetHMAC(HASH_SHA256, []byte(nonce + b.ClientID + b.APIKey), []byte(b.APISecret)) values.Set("signature", strings.ToUpper(HexEncodeToString(hmac))) path = BITSTAMP_API_URL + path diff --git a/btcchinahttp.go b/btcchinahttp.go index 1a09bdf8..b9ad6ae6 100644 --- a/btcchinahttp.go +++ b/btcchinahttp.go @@ -3,7 +3,6 @@ package main import ( "net/url" "strconv" - "crypto/sha1" "encoding/json" "errors" "strings" @@ -676,7 +675,7 @@ func (b *BTCChina) SendAuthenticatedHTTPRequest(method string, params []interfac log.Println(encoded) } - hmac := GetHMAC(sha1.New, []byte(encoded), []byte(b.APISecret)) + hmac := GetHMAC(HASH_SHA1, []byte(encoded), []byte(b.APISecret)) postData := make(map[string]interface{}) postData["method"] = method postData["params"] = params diff --git a/btcehttp.go b/btcehttp.go index 98554eb9..d853d194 100644 --- a/btcehttp.go +++ b/btcehttp.go @@ -3,7 +3,6 @@ package main import ( "net/url" "strconv" - "crypto/sha512" "strings" "time" "fmt" @@ -234,7 +233,7 @@ func (b *BTCE) SendAuthenticatedHTTPRequest(method string, values url.Values) (e values.Set("method", method) encoded := values.Encode() - hmac := GetHMAC(sha512.New, []byte(encoded), []byte(b.APISecret)) + hmac := GetHMAC(HASH_SHA512, []byte(encoded), []byte(b.APISecret)) if b.Verbose { log.Printf("Sending POST request to %s calling method %s with params %s\n", BTCE_API_PRIVATE_URL, method, encoded) diff --git a/btcmarkets.go b/btcmarkets.go index 78782d15..9474b764 100644 --- a/btcmarkets.go +++ b/btcmarkets.go @@ -2,7 +2,6 @@ package main import ( "strconv" - "crypto/sha512" "strings" "time" "log" @@ -101,7 +100,7 @@ func (b *BTCMarkets) SendAuthenticatedRequest(reqType, path, data string) (error request = path + "\n" + nonce + "\n" } - hmac := GetHMAC(sha512.New, []byte(request), []byte(b.APISecret)) + hmac := GetHMAC(HASH_SHA512, []byte(request), []byte(b.APISecret)) if b.Verbose { log.Printf("Sending %s request to %s path %s with params %s\n", reqType, BTCMARKETS_API_URL + path, path, request) diff --git a/common.go b/common.go index ad3a7ae7..5f4d7ab7 100644 --- a/common.go +++ b/common.go @@ -5,6 +5,7 @@ import ( "hash" "crypto/md5" "crypto/hmac" + "crypto/sha1" "crypto/sha512" "crypto/sha256" "encoding/base64" @@ -18,6 +19,13 @@ import ( "log" ) +const ( + HASH_SHA1 = iota + HASH_SHA256 + HASH_SHA512 + HASH_SHA512_384 +) + func GetMD5(input []byte) ([]byte) { hash := md5.New() hash.Write(input) @@ -36,7 +44,24 @@ func GetSHA256(input []byte) ([]byte) { return sha.Sum(nil) } -func GetHMAC(hash func() hash.Hash, input, key []byte) ([]byte) { +func GetHMAC(hashType int, input, key []byte) ([]byte) { + var hash func() hash.Hash + + switch hashType { + case HASH_SHA1: { + hash = sha1.New + } + case HASH_SHA256: { + hash = sha256.New + } + case HASH_SHA512: { + hash = sha512.New + } + case HASH_SHA512_384: { + hash = sha512.New384 + } + } + hmac := hmac.New(hash, []byte(key)) hmac.Write(input) return hmac.Sum(nil) diff --git a/itbithttp.go b/itbithttp.go index 05dd5eed..dacfbcbb 100644 --- a/itbithttp.go +++ b/itbithttp.go @@ -3,7 +3,6 @@ package main import ( "net/http" "strconv" - "crypto/sha512" "errors" "strings" "time" @@ -268,7 +267,7 @@ func (i *ItBit) SendAuthenticatedHTTPRequest(method string, path string, params log.Printf("Request JSON: %s\n", PayloadJson) } - hmac := GetHMAC(sha512.New, []byte(nonce + string(PayloadJson)), []byte(i.APISecret)) + hmac := GetHMAC(HASH_SHA512, []byte(nonce + string(PayloadJson)), []byte(i.APISecret)) signature := Base64Encode([]byte(HexEncodeToString(hmac))) headers := make(map[string]string) diff --git a/kraken.go b/kraken.go index d3732f1b..01aea4a7 100644 --- a/kraken.go +++ b/kraken.go @@ -5,7 +5,6 @@ import ( "fmt" "strconv" "encoding/json" - "crypto/sha512" "errors" "time" "strings" @@ -503,7 +502,7 @@ func (k *Kraken) SendAuthenticatedHTTPRequest(method string, values url.Values) } shasum := GetSHA256([]byte(values.Get("nonce") + values.Encode())) - signature := Base64Encode(GetHMAC(sha512.New, append([]byte(path), shasum...), secret)) + signature := Base64Encode(GetHMAC(HASH_SHA512, append([]byte(path), shasum...), secret)) if k.Verbose { log.Printf("Sending POST request to %s, path: %s.", KRAKEN_API_URL, path) diff --git a/lakebtchttp.go b/lakebtchttp.go index 33cec4e8..3cb78962 100644 --- a/lakebtchttp.go +++ b/lakebtchttp.go @@ -3,7 +3,6 @@ package main import ( "net/url" "strconv" - "crypto/sha256" "errors" "strings" "time" @@ -174,7 +173,7 @@ func (l *LakeBTC) SendAuthenticatedHTTPRequest(method, params string) (err error v.Set("params", params) encoded := v.Encode() - hmac := GetHMAC(sha256.New, []byte(encoded), []byte(l.APISecret)) + hmac := GetHMAC(HASH_SHA256, []byte(encoded), []byte(l.APISecret)) if l.Verbose { log.Printf("Sending POST request to %s calling method %s with params %s\n", LAKEBTC_API_URL, method, encoded)