mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 05:23:19 -07:00
Begin implementing JavaScript Crypto API, add basic Uint8Array methods.
This commit is contained in:
parent
3e8063c2c7
commit
9ed0fadd24
3 changed files with 82 additions and 0 deletions
29
js/crypto.go
Normal file
29
js/crypto.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package js
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
|
||||||
|
"github.com/robertkrimen/otto"
|
||||||
|
)
|
||||||
|
|
||||||
|
func cryptoSha1(call otto.FunctionCall) otto.Value {
|
||||||
|
argv := call.ArgumentList
|
||||||
|
argc := len(argv)
|
||||||
|
if argc != 1 {
|
||||||
|
return ReportError("Crypto.sha1: expected 1 argument, %d given instead.", argc)
|
||||||
|
}
|
||||||
|
|
||||||
|
arg := argv[0]
|
||||||
|
if (!arg.IsString()) {
|
||||||
|
return ReportError("Crypto.sha1: single argument must be a string.")
|
||||||
|
}
|
||||||
|
|
||||||
|
hasher := sha1.New()
|
||||||
|
hasher.Write([]byte(arg.String()))
|
||||||
|
v, err := otto.ToValue(string(hasher.Sum(nil)))
|
||||||
|
if err != nil {
|
||||||
|
return ReportError("Crypto.sha1: could not convert to string: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
47
js/data.go
47
js/data.go
|
@ -8,6 +8,53 @@ import (
|
||||||
"github.com/robertkrimen/otto"
|
"github.com/robertkrimen/otto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func textEncode(call otto.FunctionCall) otto.Value {
|
||||||
|
argv := call.ArgumentList
|
||||||
|
argc := len(argv)
|
||||||
|
if argc != 1 {
|
||||||
|
return ReportError("textEncode: expected 1 argument, %d given instead.", argc)
|
||||||
|
}
|
||||||
|
|
||||||
|
arg := argv[0]
|
||||||
|
if (!arg.IsString()) {
|
||||||
|
return ReportError("textEncode: single argument must be a string.")
|
||||||
|
}
|
||||||
|
|
||||||
|
encoded := []byte(arg.String())
|
||||||
|
vm := otto.New()
|
||||||
|
v, err := vm.ToValue(encoded)
|
||||||
|
if err != nil {
|
||||||
|
return ReportError("textEncode: could not convert to []uint8: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func textDecode(call otto.FunctionCall) otto.Value {
|
||||||
|
argv := call.ArgumentList
|
||||||
|
argc := len(argv)
|
||||||
|
if argc != 1 {
|
||||||
|
return ReportError("textDecode: expected 1 argument, %d given instead.", argc)
|
||||||
|
}
|
||||||
|
|
||||||
|
arg, err := argv[0].Export()
|
||||||
|
if err != nil {
|
||||||
|
return ReportError("textDecode: could not export argument value:", err.Error())
|
||||||
|
}
|
||||||
|
byteArr, ok := arg.([]byte)
|
||||||
|
if !ok {
|
||||||
|
return ReportError("textDecode: single argument must be of type []uint8.", argc)
|
||||||
|
}
|
||||||
|
|
||||||
|
decoded := string(byteArr)
|
||||||
|
v, err := otto.ToValue(decoded)
|
||||||
|
if err != nil {
|
||||||
|
return ReportError("textDecode: could not convert to string: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func btoa(call otto.FunctionCall) otto.Value {
|
func btoa(call otto.FunctionCall) otto.Value {
|
||||||
argv := call.ArgumentList
|
argv := call.ArgumentList
|
||||||
argc := len(argv)
|
argc := len(argv)
|
||||||
|
|
|
@ -27,10 +27,16 @@ func init() {
|
||||||
plugin.Defines["log_error"] = log_error
|
plugin.Defines["log_error"] = log_error
|
||||||
plugin.Defines["log_fatal"] = log_fatal
|
plugin.Defines["log_fatal"] = log_fatal
|
||||||
|
|
||||||
|
plugin.Defines["Crypto"] = map[string]interface{}{
|
||||||
|
"sha1": cryptoSha1,
|
||||||
|
}
|
||||||
|
|
||||||
plugin.Defines["btoa"] = btoa
|
plugin.Defines["btoa"] = btoa
|
||||||
plugin.Defines["atob"] = atob
|
plugin.Defines["atob"] = atob
|
||||||
plugin.Defines["gzipCompress"] = gzipCompress
|
plugin.Defines["gzipCompress"] = gzipCompress
|
||||||
plugin.Defines["gzipDecompress"] = gzipDecompress
|
plugin.Defines["gzipDecompress"] = gzipDecompress
|
||||||
|
plugin.Defines["textEncode"] = textEncode
|
||||||
|
plugin.Defines["textDecode"] = textDecode
|
||||||
|
|
||||||
plugin.Defines["httpRequest"] = httpRequest
|
plugin.Defines["httpRequest"] = httpRequest
|
||||||
plugin.Defines["http"] = httpPackage{}
|
plugin.Defines["http"] = httpPackage{}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue