-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdecrypt.go
82 lines (72 loc) · 1.83 KB
/
decrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"syscall/js"
"filippo.io/age"
"filippo.io/age/armor"
)
// Decrypt decrypts a Armored string into a string result
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
}
var identities = args[0].String()
var input = args[1].String()
ids, err := age.ParseIdentities(strings.NewReader(identities))
if err != nil {
output["error"] = err.Error()
return output
}
buff := bytes.NewBuffer(nil)
err = decrypt(ids, strings.NewReader(input), buff)
if err != nil {
output["error"] = err.Error()
return output
}
output["output"] = buff.String()
return output
}
// DecryptBinary decrypts binary data (Uint8Array) into a Uint8Array result
func DecryptBinary(this js.Value, args []js.Value) interface{} {
if len(args) != 2 {
return fmt.Errorf("invalid arguments. expected: identities, input")
}
var identities = args[0].String()
ids, err := age.ParseIdentities(strings.NewReader(identities))
if err != nil {
return err.Error()
}
input := make([]byte, args[1].Length())
js.CopyBytesToGo(input, args[1])
buff := bytes.NewBuffer(nil)
err = decrypt(ids, bytes.NewReader(input), buff)
if err != nil {
return err.Error()
}
result := js.Global().Get("Uint8Array").New(buff.Len())
js.CopyBytesToJS(result, buff.Bytes())
return result
}
// decrypt internal helper
func decrypt(keys []age.Identity, in io.Reader, out io.Writer) error {
rr := bufio.NewReader(in)
if start, _ := rr.Peek(len(armor.Header)); string(start) == armor.Header {
in = armor.NewReader(rr)
} else {
in = rr
}
r, err := age.Decrypt(in, keys...)
if err != nil {
return err
}
if _, err := io.Copy(out, r); err != nil {
return err
}
return nil
}