Skip to content

Commit

Permalink
add sha256 to pkg sha
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlys committed Oct 30, 2018
1 parent aca8a41 commit 2f3a88b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
# webcrypto

**WORK IN PROGRESS**

Golang wrapper around the WebCryptoAPI

## Installation

With a correctly configured [Go toolchain](https://golang.org/doc/install):

```
$ go get -u github.com/gopherjs/gopherjs
$ go get -u github.com/kahlys/webcrypto
```

## Testing

Go to the test directory, compile using gopherjs, then open `test.html` with a browser and look results at the console.

```
$ cd test
$ gopherjs build test.go
```

## Packages

- `sha`: `sha-256`
37 changes: 37 additions & 0 deletions sha/sha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sha

import (
"fmt"

"github.com/gopherjs/gopherjs/js"
)

// Sum256 returns the SHA-256 of the data
func Sum256(data []byte) (res []byte, err error) {
return sum("SHA-256", data)
}

func sum(hash string, data []byte) (res []byte, err error) {
crypt := js.Global.Get("crypto")
if crypt == js.Undefined {
crypt = js.Global.Get("msCrypto")
}
crypto := crypt.Get("subtle")
if crypto != js.Undefined {
if crypto.Get("digest") != js.Undefined {
resChan := make(chan []byte, 1)
algorithm := js.M{
"name": hash,
}
promise := crypto.Call("digest", algorithm, data)
promise.Call("then", func(result *js.Object) {
go func() {
resChan <- js.Global.Get("Uint8Array").New(result).Interface().([]byte)
}()
})
res = <-resChan
return res, nil
}
}
return nil, fmt.Errorf("webcrypto api error: unable to get js.crypto.subtle.digest or js.msCrypto.subtle.digest")
}
51 changes: 51 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"bytes"
"encoding/hex"
"fmt"

"github.com/kahlys/webcrypto/sha"
)

func testSha256() error {
expected, _ := hex.DecodeString("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")
actual, err := sha.Sum256([]byte("hello world"))
if err != nil {
return fmt.Errorf("sum256 error: %s", err)
}
if bytes.Compare(expected, actual) != 0 {
return fmt.Errorf("not expected value")
}
return nil
}

func main() {
register(
testfunc{"SHA-256", testSha256},
)
run()
}

type testfunc struct {
name string
test func() error
}

var testset []testfunc

func register(tests ...testfunc) {
testset = append(testset, tests...)
}

func run() {
status := "SUCCESS"
for _, t := range testset {
fmt.Println("==", t.name)
if err := t.test(); err != nil {
fmt.Println(err)
status = "FAILED"
}
}
fmt.Printf("\nTESTS STATUS: %s\n", status)
}
10 changes: 10 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>WebCrypto Test</title>
</head>
<body>
<h1>Test</h1>
<script src="test.js"></script>
</body>
</html>

0 comments on commit 2f3a88b

Please sign in to comment.