mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-20 23:16:49 +00:00
Refactored common HMAC function.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
27
common.go
27
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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user