Skip to content

Commit

Permalink
Return ArrayBuffer from hasher.digest('binary')
Browse files Browse the repository at this point in the history
This is a breaking change part of #1020.
  • Loading branch information
Ivan Mirić committed Apr 6, 2021
1 parent 5db7729 commit 51df965
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions js/modules/k6/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (hasher *Hasher) Update(input interface{}) {
// Digest returns the hash value in the given encoding.
func (hasher *Hasher) Digest(outputEncoding string) interface{} {
sum := hasher.hash.Sum(nil)
rt := common.GetRuntime(hasher.ctx)

switch outputEncoding {
case "base64":
Expand All @@ -195,11 +196,12 @@ func (hasher *Hasher) Digest(outputEncoding string) interface{} {
return hex.EncodeToString(sum)

case "binary":
return sum
ab := rt.NewArrayBuffer(sum)
return &ab

default:
err := errors.New("Invalid output encoding: " + outputEncoding)
common.Throw(common.GetRuntime(hasher.ctx), err)
common.Throw(rt, err)
}

return ""
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func TestOutputEncoding(t *testing.T) {
return true;
}
var resultBinary = hasher.digest("binary");
var resultBinary = new Uint8Array(hasher.digest("binary"));
if (!arraysEqual(resultBinary, correctBinary)) {
throw new Error("Binary encoding mismatch: " + JSON.stringify(resultBinary));
}
Expand Down

0 comments on commit 51df965

Please sign in to comment.