-
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.
adding support for stream encryption in wasm through asynchronous byt…
…e chunk exchange
- Loading branch information
Showing
13 changed files
with
370 additions
and
122 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,3 +1,4 @@ | ||
.vscode | ||
dist/ | ||
wasm_exec.js | ||
wasm_exec.js | ||
.ignore/ |
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
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,143 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"sync" | ||
"syscall/js" | ||
|
||
"dev.shib.me/xipher/utils" | ||
) | ||
|
||
const ( | ||
ctMinLegthRequired = 128 * 1024 | ||
readableBlockSize = 32 * 1024 | ||
) | ||
|
||
func decryptStr(args []js.Value) (any, error) { | ||
if len(args) != 2 { | ||
return nil, fmt.Errorf("supported arguments: secret key or password (required), ciphertext (required)") | ||
} | ||
secretKeyOrPwd := args[0].String() | ||
ciphertext := args[1].String() | ||
message, err := utils.DecryptData(secretKeyOrPwd, ciphertext) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return string(message), nil | ||
} | ||
|
||
var ( | ||
decrypters = make(map[int]*decrypter) | ||
decryptersMu sync.Mutex | ||
decrypterId int = 1 | ||
) | ||
|
||
type decrypter struct { | ||
keyOrPwd string | ||
reader io.Reader | ||
src *bytes.Buffer | ||
} | ||
|
||
func (d *decrypter) readMax(all bool) ([]byte, error) { | ||
if all { | ||
return io.ReadAll(d.reader) | ||
} | ||
buf := new(bytes.Buffer) | ||
if d.src.Len() >= ctMinLegthRequired { | ||
if d.reader == nil { | ||
reader, err := utils.DecryptingReader(d.keyOrPwd, d.src) | ||
if err != nil { | ||
return nil, err | ||
} | ||
d.reader = reader | ||
} | ||
block := make([]byte, readableBlockSize) | ||
for d.src.Len() >= ctMinLegthRequired { | ||
n, err := d.reader.Read(block) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if n == 0 { | ||
break | ||
} | ||
buf.Write(block[:]) | ||
} | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func (d *decrypter) read(data []byte) ([]byte, error) { | ||
if _, err := d.src.Write(data); err != nil { | ||
return nil, err | ||
} | ||
return d.readMax(false) | ||
} | ||
|
||
func (d *decrypter) close() ([]byte, error) { | ||
return d.readMax(true) | ||
} | ||
|
||
func newStreamDecrypter(args []js.Value) (any, error) { | ||
if len(args) != 1 { | ||
return nil, fmt.Errorf("supported arguments: secret key or password (required)") | ||
} | ||
decryptersMu.Lock() | ||
defer decryptersMu.Unlock() | ||
keyOrPwd := args[0].String() | ||
dec := &decrypter{ | ||
keyOrPwd: keyOrPwd, | ||
src: new(bytes.Buffer), | ||
} | ||
id := decrypterId | ||
decrypters[id] = dec | ||
decrypterId++ | ||
return id, nil | ||
} | ||
|
||
func readFromDecrypter(args []js.Value) (any, error) { | ||
if len(args) != 2 { | ||
return nil, fmt.Errorf("supported arguments: id (required), input (required)") | ||
} | ||
decryptersMu.Lock() | ||
id := args[0].Int() | ||
inputJSArray := args[1] | ||
dec, ok := decrypters[id] | ||
decryptersMu.Unlock() | ||
if !ok { | ||
return nil, fmt.Errorf("decrypter not found for id: %d", id) | ||
} | ||
inputLength := inputJSArray.Get("length").Int() | ||
inputData := make([]byte, inputLength) | ||
js.CopyBytesToGo(inputData, inputJSArray) | ||
outputData, err := dec.read(inputData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
outputJSArray := js.Global().Get("Uint8Array").New(len(outputData)) | ||
js.CopyBytesToJS(outputJSArray, outputData) | ||
return outputJSArray, nil | ||
} | ||
|
||
func closeDecrypter(args []js.Value) (any, error) { | ||
if len(args) != 1 { | ||
return nil, fmt.Errorf("supported arguments: id (required)") | ||
} | ||
decryptersMu.Lock() | ||
id := args[0].Int() | ||
dec, ok := decrypters[id] | ||
if !ok { | ||
decryptersMu.Unlock() | ||
return nil, fmt.Errorf("decrypter not found for id: %d", id) | ||
} | ||
delete(decrypters, id) | ||
decryptersMu.Unlock() | ||
outputData, err := dec.close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
outputJSArray := js.Global().Get("Uint8Array").New(len(outputData)) | ||
js.CopyBytesToJS(outputJSArray, outputData) | ||
return outputJSArray, 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,122 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"sync" | ||
"syscall/js" | ||
|
||
"dev.shib.me/xipher/utils" | ||
) | ||
|
||
func encryptStr(args []js.Value) (any, error) { | ||
if len(args) != 2 { | ||
return nil, fmt.Errorf("supported arguments: public key, secret key or password (required), message (required)") | ||
} | ||
keyOrPwd := args[0].String() | ||
message := args[1].String() | ||
ciphertext, err := utils.EncryptData(keyOrPwd, []byte(message), true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ciphertext, nil | ||
} | ||
|
||
var ( | ||
encrypters = make(map[int]*encrypter) | ||
encryptersMu sync.Mutex | ||
encrypterId int = 1 | ||
) | ||
|
||
type encrypter struct { | ||
writer io.WriteCloser | ||
dst *bytes.Buffer | ||
} | ||
|
||
func (e *encrypter) write(data []byte) ([]byte, error) { | ||
_, err := e.writer.Write(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if e.dst.Len() > 0 { | ||
return e.dst.Next(e.dst.Len()), nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (e *encrypter) close() ([]byte, error) { | ||
err := e.writer.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return e.dst.Next(e.dst.Len()), nil | ||
} | ||
|
||
func newStreamEncrypter(args []js.Value) (any, error) { | ||
if len(args) != 2 { | ||
return nil, fmt.Errorf("supported arguments: public key, secret key or password (required), compress (required)") | ||
} | ||
encryptersMu.Lock() | ||
defer encryptersMu.Unlock() | ||
keyOrPwd := args[0].String() | ||
compress := args[1].Bool() | ||
enc := &encrypter{ | ||
dst: new(bytes.Buffer), | ||
} | ||
writer, err := utils.EncryptingWriter(keyOrPwd, enc.dst, compress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
enc.writer = writer | ||
id := encrypterId | ||
encrypters[id] = enc | ||
encrypterId++ | ||
return id, nil | ||
} | ||
|
||
func writeToEncrypter(args []js.Value) (any, error) { | ||
if len(args) != 2 { | ||
return nil, fmt.Errorf("supported arguments: id (required), input (required)") | ||
} | ||
encryptersMu.Lock() | ||
id := args[0].Int() | ||
inputJSArray := args[1] | ||
enc, ok := encrypters[id] | ||
encryptersMu.Unlock() | ||
if !ok { | ||
return nil, fmt.Errorf("encrypter not found for id: %d", id) | ||
} | ||
inputLength := inputJSArray.Get("length").Int() | ||
inputData := make([]byte, inputLength) | ||
js.CopyBytesToGo(inputData, inputJSArray) | ||
outputData, err := enc.write(inputData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
outputJSArray := js.Global().Get("Uint8Array").New(len(outputData)) | ||
js.CopyBytesToJS(outputJSArray, outputData) | ||
return outputJSArray, nil | ||
} | ||
|
||
func closeEncrypter(args []js.Value) (any, error) { | ||
if len(args) != 1 { | ||
return nil, fmt.Errorf("supported arguments: id (required)") | ||
} | ||
encryptersMu.Lock() | ||
id := args[0].Int() | ||
enc, ok := encrypters[id] | ||
if !ok { | ||
encryptersMu.Unlock() | ||
return nil, fmt.Errorf("encrypter not found for id: %d", id) | ||
} | ||
delete(encrypters, id) | ||
encryptersMu.Unlock() | ||
outputData, err := enc.close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
outputJSArray := js.Global().Get("Uint8Array").New(len(outputData)) | ||
js.CopyBytesToJS(outputJSArray, outputData) | ||
return outputJSArray, nil | ||
} |
Oops, something went wrong.