Skip to content

Commit

Permalink
Merge pull request grafana#341 from loadimpact/feature/crypto-base64-…
Browse files Browse the repository at this point in the history
…encodings

Add support for base64 URL and raw-URL output encoding to hashers
  • Loading branch information
liclac authored Oct 18, 2017
2 parents 35dba63 + ec0d920 commit dcee9f3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions js/modules/k6/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ func (hasher *Hasher) Digest(outputEncoding string) string {
case "base64":
return base64.StdEncoding.EncodeToString(sum)

case "base64url":
return base64.URLEncoding.EncodeToString(sum)

case "base64rawurl":
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(sum)

case "hex":
return hex.EncodeToString(sum)

Expand Down
15 changes: 14 additions & 1 deletion js/modules/k6/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ func TestOutputEncoding(t *testing.T) {
_, err := common.RunString(rt, `
const correctHex = "5eb63bbbe01eeed093cb22bb8f5acdc3";
const correctBase64 = "XrY7u+Ae7tCTyyK7j1rNww==";
const correctBase64URL = "XrY7u-Ae7tCTyyK7j1rNww=="
const correctBase64RawURL = "XrY7u-Ae7tCTyyK7j1rNww";
let hasher = crypto.createHash("md5");
hasher.update("hello world");
Expand All @@ -238,7 +240,18 @@ func TestOutputEncoding(t *testing.T) {
const resultBase64 = hasher.digest("base64");
if (resultBase64 !== correctBase64) {
throw new Error("Base64 encoding mismatch: " + resultBase64);
}`)
}
const resultBase64URL = hasher.digest("base64url");
if (resultBase64URL !== correctBase64URL) {
throw new Error("Base64 URL encoding mismatch: " + resultBase64URL);
}
const resultBase64RawURL = hasher.digest("base64rawurl");
if (resultBase64RawURL !== correctBase64RawURL) {
throw new Error("Base64 raw URL encoding mismatch: " + resultBase64RawURL);
}
`)

assert.NoError(t, err)
})
Expand Down

0 comments on commit dcee9f3

Please sign in to comment.