以下代码编译不能通过,下面是提示和源码,请求大佬帮忙看下
```
PS D:\Program Files\Go\src\gowork> $env:GOARCH="wasm";$env:GOOS="js";go build -o crypto.wasm crypto.go
# command-line-arguments
.\crypto.go:26:15: cannot convert args[0] (type js.Value) to type []byte
.\crypto.go:27:15: cannot convert args[1] (type js.Value) to type []byte
.\crypto.go:40:15: cannot convert args[0] (type js.Value) to type []byte
.\crypto.go:41:15: cannot convert args[1] (type js.Value) to type []byte
.\crypto.go:53:11: js.Global.Set undefined (type func() js.Value has no field or method Set)
.\crypto.go:54:11: js.Global.Set undefined (type func() js.Value has no field or method Set)
```
```
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"syscall/js"
)
func padding(src []byte, blockSize int) []byte {
padNum := blockSize - len(src)%blockSize
pad := bytes.Repeat([]byte{byte(padNum)}, padNum)
return append(src, pad...)
}
func unpadding(src []byte) []byte {
n := len(src)
unPadNum := int(src[n-1])
return src[:n-unPadNum]
}
func encryptAES(this js.Value,args []js.Value) (interface{}) {
src := []byte(args[0])
key := []byte(args[1])
block, err := aes.NewCipher(key)
if err != nil {
return ""
}
src = padding(src, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, key)
blockMode.CryptBlocks(src, src)
return string(src)
}
func decryptAES(this js.Value,args []js.Value) (interface{}) {
src := []byte(args[0])
key := []byte(args[1])
block, err := aes.NewCipher(key)
if err != nil {
return ""
}
blockMode := cipher.NewCBCDecrypter(block, key)
blockMode.CryptBlocks(src, src)
src = unpadding(src)
return string(src)
}
func main() {
js.Global.Set("encryptAES", js.FuncOf(encryptAES))
js.Global.Set("decryptAES", js.FuncOf(decryptAES))
}
```
更多评论
.\crypto.go:26:15: cannot convert args[0] (type js.Value) to type []byte
这不明摆着?
#1