Skip to content

Commit

Permalink
Replace fastsha256 with crypto/sha256 (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
dajohi authored and alexlyp committed Aug 24, 2016
1 parent 1c9d038 commit 717040c
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion chaincfg/chainhash/hashfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// HashFunc calculates the hash of the supplied bytes.
// TODO(jcv) Should modify blake256 so it has the same interface as blake2
// and fastsha256 so these function can look more like btcsuite. Then should
// and sha256 so these function can look more like btcsuite. Then should
// try to get it to the upstream blake256 repo
func HashFunc(data []byte) [blake256.Size]byte {
var outB [blake256.Size]byte
Expand Down
4 changes: 2 additions & 2 deletions dcrec/edwards/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ package edwards
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"fmt"
"hash"
"io"
"math/big"

"crypto/sha512"

"github.com/btcsuite/fastsha256"
"github.com/decred/ed25519"
"github.com/decred/ed25519/edwards25519"
)
Expand Down Expand Up @@ -102,7 +102,7 @@ func NonceRFC6979(curve *TwistedEdwardsCurve, privkey *big.Int, hash []byte,
extra []byte, version []byte) *big.Int {
q := curve.Params().N
x := privkey
alg := fastsha256.New
alg := sha256.New

qlen := q.BitLen()
holen := alg().Size()
Expand Down
4 changes: 2 additions & 2 deletions dcrec/secp256k1/schnorr/threshold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package schnorr

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"math/rand"
"testing"

"github.com/btcsuite/fastsha256"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrec/secp256k1"
"github.com/stretchr/testify/assert"
Expand All @@ -26,7 +26,7 @@ type signerHex struct {
// Sha256. The internal tests from secp256k1 are kind of screwy and for
// partial signatures call this hash function instead of testSchnorrHash.
func testSchnorrSha256Hash(msg []byte) []byte {
sha := fastsha256.Sum256(msg)
sha := sha256.Sum256(msg)
return sha[:]
}

Expand Down
5 changes: 2 additions & 3 deletions dcrec/secp256k1/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/hmac"
"crypto/sha256"
"errors"
"fmt"
"hash"
"math/big"

"github.com/btcsuite/fastsha256"
)

// Errors returned by canonicalPadding.
Expand Down Expand Up @@ -455,7 +454,7 @@ func NonceRFC6979(privkey *big.Int, hash []byte, extra []byte,
curve := S256()
q := curve.Params().N
x := privkey
alg := fastsha256.New
alg := sha256.New

qlen := q.BitLen()
holen := alg().Size()
Expand Down
5 changes: 2 additions & 3 deletions dcrec/secp256k1/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ package secp256k1_test
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"math/big"
"testing"

"github.com/decred/dcrd/dcrec/secp256k1"

"github.com/btcsuite/fastsha256"
)

type signatureTest struct {
Expand Down Expand Up @@ -561,7 +560,7 @@ func TestRFC6979(t *testing.T) {

for i, test := range tests {
privKey, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), decodeHex(test.key))
hash := fastsha256.Sum256([]byte(test.msg))
hash := sha256.Sum256([]byte(test.msg))

// Ensure deterministically generated nonce is the expected value.
gotNonce := secp256k1.TstNonceRFC6979(privKey.D, hash[:]).Bytes()
Expand Down
18 changes: 8 additions & 10 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package: github.com/decred/dcrd
import:
- package: github.com/btcsuite/btclog
- package: github.com/btcsuite/fastsha256
- package: github.com/btcsuite/go-flags
- package: github.com/btcsuite/go-socks
subpackages:
Expand Down
12 changes: 6 additions & 6 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main

import (
"bytes"
"crypto/sha256"
"crypto/subtle"
"crypto/tls"
"encoding/base64"
Expand All @@ -30,7 +31,6 @@ import (
"sync/atomic"
"time"

"github.com/btcsuite/fastsha256"
"github.com/btcsuite/websocket"

"github.com/decred/bitset"
Expand Down Expand Up @@ -5723,8 +5723,8 @@ type rpcServer struct {
policy *mining.Policy
server *server
chain *blockchain.BlockChain
authsha [fastsha256.Size]byte
limitauthsha [fastsha256.Size]byte
authsha [sha256.Size]byte
limitauthsha [sha256.Size]byte
ntfnMgr *wsNotificationManager
numClients int32
statusLines map[int]string
Expand Down Expand Up @@ -5881,7 +5881,7 @@ func (s *rpcServer) checkAuth(r *http.Request, require bool) (bool, bool, error)
return false, false, nil
}

authsha := fastsha256.Sum256([]byte(authhdr[0]))
authsha := sha256.Sum256([]byte(authhdr[0]))

// Check for limited auth first as in environments with limited users, those
// are probably expected to have a higher volume of calls
Expand Down Expand Up @@ -6218,12 +6218,12 @@ func newRPCServer(listenAddrs []string, policy *mining.Policy, s *server) (*rpcS
if cfg.RPCUser != "" && cfg.RPCPass != "" {
login := cfg.RPCUser + ":" + cfg.RPCPass
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
rpc.authsha = fastsha256.Sum256([]byte(auth))
rpc.authsha = sha256.Sum256([]byte(auth))
}
if cfg.RPCLimitUser != "" && cfg.RPCLimitPass != "" {
login := cfg.RPCLimitUser + ":" + cfg.RPCLimitPass
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
rpc.limitauthsha = fastsha256.Sum256([]byte(auth))
rpc.limitauthsha = sha256.Sum256([]byte(auth))
}
rpc.ntfnMgr = newWsNotificationManager(&rpc)

Expand Down
4 changes: 2 additions & 2 deletions rpcwebsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
import (
"bytes"
"container/list"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/hex"
Expand All @@ -20,7 +21,6 @@ import (
"sync"
"time"

"github.com/btcsuite/fastsha256"
"github.com/btcsuite/golangcrypto/ripemd160"
"github.com/btcsuite/websocket"

Expand Down Expand Up @@ -1325,7 +1325,7 @@ func (c *wsClient) handleMessage(msg []byte) {
// Check credentials.
login := authCmd.Username + ":" + authCmd.Passphrase
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
authSha := fastsha256.Sum256([]byte(auth))
authSha := sha256.Sum256([]byte(auth))
cmp := subtle.ConstantTimeCompare(authSha[:], c.server.authsha[:])
limitcmp := subtle.ConstantTimeCompare(authSha[:], c.server.limitauthsha[:])
if cmp != 1 && limitcmp != 1 {
Expand Down

0 comments on commit 717040c

Please sign in to comment.