-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
4 changed files
with
123 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 |
---|---|---|
@@ -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` |
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,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") | ||
} |
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,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) | ||
} |
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,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>WebCrypto Test</title> | ||
</head> | ||
<body> | ||
<h1>Test</h1> | ||
<script src="test.js"></script> | ||
</body> | ||
</html> |