Created
November 16, 2022 20:02
-
-
Save neezer/c366a4bde001577696d471dd33447a45 to your computer and use it in GitHub Desktop.
Age WASM for Uint8Array data
This file contains hidden or 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
Modified from https://github.com/MarinX/agewasm |
This file contains hidden or 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
package main | |
import ( | |
"bytes" | |
"io" | |
"strings" | |
"syscall/js" | |
"filippo.io/age" | |
) | |
func Decrypt(this js.Value, args []js.Value) interface{} { | |
output := make(map[string]interface{}) | |
if len(args) != 2 { | |
output["error"] = "invalid arguments. expected: identities, input" | |
return output | |
} | |
identities := args[0].String() | |
input := make([]byte, args[1].Get("length").Int()) | |
buff := bytes.NewBuffer(nil) | |
ids, err := age.ParseIdentities(strings.NewReader(identities)) | |
if err != nil { | |
output["error"] = err.Error() | |
return output | |
} | |
js.CopyBytesToGo(input, args[1]) | |
err = decrypt(ids, bytes.NewReader(input), buff) | |
if err != nil { | |
output["error"] = err.Error() | |
return output | |
} | |
result := js.Global().Get("Uint8Array").New(buff.Len()) | |
js.CopyBytesToJS(result, buff.Bytes()) | |
output["output"] = result | |
return output | |
} | |
// decrypt internal helper | |
func decrypt(keys []age.Identity, in io.Reader, out io.Writer) error { | |
r, err := age.Decrypt(in, keys...) | |
if err != nil { | |
return err | |
} | |
if _, err := io.Copy(out, r); err != nil { | |
return err | |
} | |
return nil | |
} |
This file contains hidden or 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
package main | |
import ( | |
"bytes" | |
"io" | |
"strings" | |
"syscall/js" | |
"filippo.io/age" | |
) | |
func Encrypt(this js.Value, args []js.Value) interface{} { | |
output := make(map[string]interface{}) | |
if len(args) != 2 { | |
output["error"] = "invalid arguments. expected: recipients, input" | |
return output | |
} | |
recipients := args[0].String() | |
buff := bytes.NewBuffer(nil) | |
input := make([]byte, args[1].Get("length").Int()) | |
js.CopyBytesToGo(input, args[1]) | |
ids, err := age.ParseRecipients(strings.NewReader(recipients)) | |
if err != nil { | |
output["error"] = err.Error() | |
return output | |
} | |
err = encrypt(ids, bytes.NewReader(input), buff) | |
if err != nil { | |
output["error"] = err.Error() | |
return output | |
} | |
result := js.Global().Get("Uint8Array").New(buff.Len()) | |
js.CopyBytesToJS(result, buff.Bytes()) | |
output["output"] = result | |
return output | |
} | |
// encrypt internal helper | |
func encrypt(recipients []age.Recipient, in io.Reader, out io.Writer) error { | |
var a io.WriteCloser | |
w, err := age.Encrypt(out, recipients...) | |
if err != nil { | |
return err | |
} | |
if _, err := io.Copy(w, in); err != nil { | |
return err | |
} | |
if err := w.Close(); err != nil { | |
return err | |
} | |
if a != nil { | |
if err := a.Close(); err != nil { | |
return err | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment