-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S2053: Password hashing functions should use an unpredict…
…able salt (#4648) * Add go to rule S2053 * Add description for S2053 for Go --------- Co-authored-by: daniel-teuchert-sonarsource <[email protected]> Co-authored-by: Daniel Teuchert <[email protected]> Co-authored-by: daniel-teuchert-sonarsource <[email protected]>
- Loading branch information
1 parent
1f6167e
commit d9e2903
Showing
2 changed files
with
73 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
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,71 @@ | ||
include::../summary.adoc[] | ||
|
||
== Why is this an issue? | ||
|
||
include::../rationale.adoc[] | ||
|
||
include::../impact.adoc[] | ||
|
||
include::../exceptions.adoc[] | ||
|
||
// How to fix it section | ||
|
||
== How to fix it | ||
|
||
=== Code examples | ||
|
||
include::../common/fix/code-rationale.adoc[] | ||
|
||
==== Noncompliant code example | ||
|
||
[source,go,diff-id=1,diff-type=noncompliant] | ||
---- | ||
import ( | ||
"crypto/sha256" | ||
"golang.org/x/crypto/pbkdf2" | ||
) | ||
func example(password []byte) { | ||
pbkdf2.Key(password, []byte("fixedSalt"), 4096, 32, sha256.New) // Noncompliant | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,go,diff-id=1,diff-type=compliant] | ||
---- | ||
import ( | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"golang.org/x/crypto/pbkdf2" | ||
) | ||
func example(password []byte) { | ||
randomSalt := make([]byte, 32) | ||
rand.Read(randomSalt) | ||
pbkdf2.Key(password, randomSalt, 4096, 32, sha256.New) | ||
} | ||
---- | ||
|
||
=== How does this work? | ||
|
||
include::../common/fix/salt.adoc[] | ||
|
||
Here, the compliant code example ensures the salt is random and has a sufficient | ||
length by calling the `crypto.rand.Read` function. This function internally | ||
uses a cryptographically secure pseudo-random number generator. | ||
|
||
|
||
== Resources | ||
|
||
include::../common/resources/standards.adoc[] | ||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
include::../message.adoc[] | ||
|
||
endif::env-github,rspecator-view[] |