-
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
8 changed files
with
139 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,2 @@ | ||
wasm_exec.js | ||
*.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,12 @@ | ||
import wasm from "./jwe.wasm?url"; | ||
|
||
import("./wasm_exec.js").then(() => { | ||
const go = new Go(); | ||
void WebAssembly.instantiateStreaming(fetch(wasm), go.importObject).then((obj) => { | ||
return go.run(obj.instance); | ||
}); | ||
}); | ||
|
||
export const encrypt = async (payload, key) => { | ||
return await jweEncrypt(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 @@ | ||
//go:build wasm | ||
|
||
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("jweEncrypt", js.FuncOf(func(this js.Value, args []js.Value) any { | ||
if len(args) != 2 { | ||
return nil | ||
} | ||
ret, err := encrypt(args[0].String(), args[1].String()) | ||
if err != nil { | ||
return nil | ||
} | ||
return ret | ||
})) | ||
|
||
select {} | ||
} | ||
|
||
func encrypt(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.2", | ||
"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 $(go env GOROOT)/misc/wasm/wasm_exec.js ./wasm_exec.js", | ||
"build:wasm": "GOOS=js GOARCH=wasm go build -o jwe.wasm main.go", | ||
"build": "bun run build:wasm && bun run cp:wasm_exec", | ||
"prepublishOnly": "bun run build" | ||
}, | ||
"type": "module" | ||
} |