You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2021/06/04 04:17:29 UTC

[servicecomb-service-center] branch master updated: optimize perf on token signature (#1039)

This is an automated email from the ASF dual-hosted git repository.

littlecui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new f305c2a  optimize perf on token signature (#1039)
f305c2a is described below

commit f305c2a7d4d3ae35c063d47db63a5b82a0fa4616
Author: Shawn <xi...@gmail.com>
AuthorDate: Fri Jun 4 12:17:19 2021 +0800

    optimize perf on token signature (#1039)
---
 pkg/privacy/password.go      |  4 +++-
 pkg/privacy/password_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/pkg/privacy/password.go b/pkg/privacy/password.go
index 98ee9b4..a431883 100644
--- a/pkg/privacy/password.go
+++ b/pkg/privacy/password.go
@@ -31,6 +31,8 @@ const (
 	algBcrypt = "$2a$"
 )
 
+var ScryptParams = scrypt.Params{N: 1024, R: 8, P: 1, SaltLen: 8, DKLen: 32}
+
 //HashPassword
 //Deprecated: use ScryptPassword, this is only for unit test to test compatible with old version
 func HashPassword(pwd string) (string, error) {
@@ -41,7 +43,7 @@ func HashPassword(pwd string) (string, error) {
 	return stringutil.Bytes2str(hash), nil
 }
 func ScryptPassword(pwd string) (string, error) {
-	hash, err := scrypt.GenerateFromPassword([]byte(pwd), scrypt.DefaultParams)
+	hash, err := scrypt.GenerateFromPassword([]byte(pwd), ScryptParams)
 	if err != nil {
 		return "", err
 	}
diff --git a/pkg/privacy/password_test.go b/pkg/privacy/password_test.go
index c4ad3dd..12abd01 100644
--- a/pkg/privacy/password_test.go
+++ b/pkg/privacy/password_test.go
@@ -18,8 +18,12 @@
 package privacy_test
 
 import (
+	"crypto/sha512"
 	"github.com/apache/servicecomb-service-center/pkg/privacy"
+	scrypt "github.com/elithrar/simple-scrypt"
+	"github.com/go-chassis/foundation/stringutil"
 	"github.com/stretchr/testify/assert"
+	"golang.org/x/crypto/pbkdf2"
 	"testing"
 )
 
@@ -36,6 +40,12 @@ func TestHashPassword(t *testing.T) {
 
 	sameMac := privacy.SamePassword(mac, "test")
 	assert.True(t, sameMac)
+
+	t.Run("use different params for scrypt, should be compatible", func(t *testing.T) {
+		h2, _ := scrypt.GenerateFromPassword([]byte("test"), scrypt.Params{N: 1024, R: 8, P: 1, SaltLen: 8, DKLen: 32})
+		same := privacy.SamePassword(stringutil.Bytes2str(h2), "test")
+		assert.True(t, same)
+	})
 }
 func BenchmarkBcrypt(b *testing.B) {
 	h, _ := privacy.HashPassword("test")
@@ -71,3 +81,38 @@ func BenchmarkScryptP(b *testing.B) {
 	})
 	b.ReportAllocs()
 }
+func BenchmarkScrypt1024(b *testing.B) {
+	p := scrypt.Params{N: 1024, R: 8, P: 1, SaltLen: 8, DKLen: 32}
+	b.RunParallel(func(pb *testing.PB) {
+		for pb.Next() {
+			_, _ = scrypt.GenerateFromPassword([]byte("test"), p)
+		}
+	})
+	b.ReportAllocs()
+}
+func BenchmarkScrypt4096(b *testing.B) {
+	p := scrypt.Params{N: 4096, R: 8, P: 1, SaltLen: 8, DKLen: 32}
+	b.RunParallel(func(pb *testing.PB) {
+		for pb.Next() {
+			_, _ = scrypt.GenerateFromPassword([]byte("test"), p)
+		}
+	})
+	b.ReportAllocs()
+}
+func BenchmarkScrypt16384(b *testing.B) {
+	b.RunParallel(func(pb *testing.PB) {
+		for pb.Next() {
+			_, _ = scrypt.GenerateFromPassword([]byte("test"), scrypt.DefaultParams)
+		}
+	})
+	b.ReportAllocs()
+}
+func BenchmarkPbkdf2(b *testing.B) {
+	salt := make([]byte, 8)
+	b.RunParallel(func(pb *testing.PB) {
+		for pb.Next() {
+			_ = pbkdf2.Key([]byte("test"), salt, 1024, 32, sha512.New)
+		}
+	})
+	b.ReportAllocs()
+}