-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0f0016
commit 0000ff6
Showing
9 changed files
with
207 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package privacy | ||
|
||
import ( | ||
"github.com/apache/servicecomb-service-center/pkg/log" | ||
"github.com/elithrar/simple-scrypt" | ||
"github.com/go-chassis/foundation/stringutil" | ||
"golang.org/x/crypto/bcrypt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
algBcrypt = "$2a$" | ||
) | ||
|
||
//HashPassword | ||
//Deprecated: use ScryptPassword, this is only for unit test to test compatible with old version | ||
func HashPassword(pwd string) (string, error) { | ||
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), 14) | ||
if err != nil { | ||
return "", err | ||
} | ||
return stringutil.Bytes2str(hash), nil | ||
} | ||
func ScryptPassword(pwd string) (string, error) { | ||
hash, err := scrypt.GenerateFromPassword([]byte(pwd), scrypt.DefaultParams) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(hash), nil | ||
} | ||
func SamePassword(hashedPwd, pwd string) bool { | ||
if strings.HasPrefix(hashedPwd, algBcrypt) { | ||
err := bcrypt.CompareHashAndPassword([]byte(hashedPwd), []byte(pwd)) | ||
if err == bcrypt.ErrMismatchedHashAndPassword { | ||
log.Warn("incorrect password attempts") | ||
} | ||
return err == nil | ||
} | ||
err := scrypt.CompareHashAndPassword([]byte(hashedPwd), []byte(pwd)) | ||
if err == scrypt.ErrMismatchedHashAndPassword { | ||
log.Warn("incorrect password attempts") | ||
} | ||
return err == nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package privacy_test | ||
|
||
import ( | ||
"github.com/apache/servicecomb-service-center/pkg/privacy" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestHashPassword(t *testing.T) { | ||
h, _ := privacy.HashPassword("test") | ||
t.Log(h) | ||
mac, _ := privacy.ScryptPassword("test") | ||
t.Log(mac) | ||
|
||
t.Run("given old hash result, should be compatible", func(t *testing.T) { | ||
same := privacy.SamePassword(h, "test") | ||
assert.True(t, same) | ||
}) | ||
|
||
sameMac := privacy.SamePassword(mac, "test") | ||
assert.True(t, sameMac) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters