-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
652 additions
and
21 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
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
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 @@ | ||
*.wasm |
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 @@ | ||
export function encrypt(payload: string, key: object): Promise<string> |
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,17 @@ | ||
import wasmURL from "./jwe.wasm?url"; | ||
|
||
let wasm; | ||
|
||
void import("./wasm_exec.js").then(() => { | ||
const go = new Go(); | ||
|
||
return WebAssembly.instantiateStreaming(fetch(wasmURL), go.importObject) | ||
.then((obj) => { | ||
wasm = obj.instance; | ||
return go.run(obj.instance); | ||
}); | ||
}); | ||
|
||
export const encrypt = async (payload, key) => { | ||
return wasm?.exports.encrypt?.(payload, JSON.stringify(key)) ?? __go_jwe_encrypt(payload, JSON.stringify(key)); | ||
}; |
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,50 @@ | ||
package main | ||
|
||
import ( | ||
"syscall/js" | ||
|
||
"github.com/lestrrat-go/jwx/v2/jwa" | ||
"github.com/lestrrat-go/jwx/v2/jwe" | ||
"github.com/lestrrat-go/jwx/v2/jwk" | ||
) | ||
|
||
func main() { | ||
js.Global().Set("__go_jwe_encrypt", js.FuncOf(func(this js.Value, args []js.Value) any { | ||
if len(args) != 2 { | ||
return nil | ||
} | ||
return encrypt(args[0].String(), args[1].String()) | ||
})) | ||
select {} | ||
} | ||
|
||
//go:wasm-module jwe | ||
//export encrypt | ||
func encrypt(payload string, kjson string) string { | ||
ret, _ := jweEncrypt(payload, kjson) | ||
return ret | ||
} | ||
|
||
func jweEncrypt(payload string, kjson string) (string, error) { | ||
key, err := jwk.ParseKey([]byte(kjson)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
pubKey, err := key.PublicKey() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
ret, err := jwe.Encrypt( | ||
[]byte(payload), | ||
jwe.WithKey(key.Algorithm(), pubKey), | ||
jwe.WithContentEncryption(jwa.A256GCM), | ||
) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(ret), nil | ||
} |
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,31 @@ | ||
{ | ||
"name": "@innoai-tech/jwe", | ||
"version": "0.1.3", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./index.d.ts", | ||
"default": "./index.mjs" | ||
} | ||
} | ||
}, | ||
"files": [ | ||
"index.d.ts", | ||
"index.mjs", | ||
"wasm_exec.js", | ||
"jwe.wasm" | ||
], | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "ssh://[email protected]:innoai-tech/nodekit.git", | ||
"directory": "nodepkg/jwe" | ||
}, | ||
"scripts": { | ||
"cp:wasm_exec": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./wasm_exec.js", | ||
"build:wasm": "tinygo build -target=wasm -gc=leaking -no-debug -o=jwe.wasm main.go", | ||
"check": "ls -ahl jwe*", | ||
"build": "bun run build:wasm && bun run check" | ||
}, | ||
"type": "module" | ||
} |
Oops, something went wrong.