support multi input for hash
This commit is contained in:
parent
cc8fce2708
commit
991b21616d
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module apigo.cc/gojs/util
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
apigo.cc/gojs v0.0.1
|
apigo.cc/gojs v0.0.3
|
||||||
github.com/ssgo/u v1.7.9
|
github.com/ssgo/u v1.7.9
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
28
util.go
28
util.go
@ -2,6 +2,10 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
|
"crypto/sha1"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/sha512"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -185,19 +189,35 @@ func init() {
|
|||||||
},
|
},
|
||||||
"md5": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"md5": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
return vm.ToValue(u.MD5(u.Bytes(args.Arguments[0].Export())))
|
hash := md5.New()
|
||||||
|
for _, v := range args.Array(0) {
|
||||||
|
hash.Write(u.Bytes(v))
|
||||||
|
}
|
||||||
|
return vm.ToValue(hash.Sum(nil))
|
||||||
},
|
},
|
||||||
"sha1": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"sha1": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
return vm.ToValue(u.Sha1(u.Bytes(args.Arguments[0].Export())))
|
hash := sha1.New()
|
||||||
|
for _, v := range args.Array(0) {
|
||||||
|
hash.Write(u.Bytes(v))
|
||||||
|
}
|
||||||
|
return vm.ToValue(hash.Sum(nil))
|
||||||
},
|
},
|
||||||
"sha256": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"sha256": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
return vm.ToValue(u.Sha256(u.Bytes(args.Arguments[0].Export())))
|
hash := sha256.New()
|
||||||
|
for _, v := range args.Array(0) {
|
||||||
|
hash.Write(u.Bytes(v))
|
||||||
|
}
|
||||||
|
return vm.ToValue(hash.Sum(nil))
|
||||||
},
|
},
|
||||||
"sha512": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"sha512": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
return vm.ToValue(u.Sha512(u.Bytes(args.Arguments[0].Export())))
|
hash := sha512.New()
|
||||||
|
for _, v := range args.Array(0) {
|
||||||
|
hash.Write(u.Bytes(v))
|
||||||
|
}
|
||||||
|
return vm.ToValue(hash.Sum(nil))
|
||||||
},
|
},
|
||||||
"tpl": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"tpl": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
||||||
|
8
util.ts
8
util.ts
@ -63,10 +63,10 @@ function gunzip(data:string): any {return null}
|
|||||||
function id(): string { return '' }
|
function id(): string { return '' }
|
||||||
function uniqueId(): string { return '' }
|
function uniqueId(): string { return '' }
|
||||||
function token(size: number): string { return '' }
|
function token(size: number): string { return '' }
|
||||||
function md5(data:any): string {return ''}
|
function md5(...data: any[]): string { return '' }
|
||||||
function sha1(data:any): string {return ''}
|
function sha1(...data: any[]): string { return '' }
|
||||||
function sha256(data:any): string {return ''}
|
function sha256(...data: any[]): string { return '' }
|
||||||
function sha512(data:any): string {return ''}
|
function sha512(...data: any[]): string { return '' }
|
||||||
function tpl(text: string, data: any, functions?: Object): string { return '' }
|
function tpl(text: string, data: any, functions?: Object): string { return '' }
|
||||||
function sleep(ms: number): void { }
|
function sleep(ms: number): void { }
|
||||||
function setTimeout(callback: () => void, ms?: number, ...args: any): void { }
|
function setTimeout(callback: () => void, ms?: number, ...args: any): void { }
|
||||||
|
@ -17,7 +17,7 @@ func TestHash(t *testing.T) {
|
|||||||
testIsSame(vm, t, "util.md5('hello 123')", u.MD5([]byte("hello 123")))
|
testIsSame(vm, t, "util.md5('hello 123')", u.MD5([]byte("hello 123")))
|
||||||
testIsSame(vm, t, "util.base64(util.md5('hello 123'))", u.MD5Base64("hello 123"))
|
testIsSame(vm, t, "util.base64(util.md5('hello 123'))", u.MD5Base64("hello 123"))
|
||||||
testIsSame(vm, t, "util.hex(util.md5('hello 123'))", u.MD5String("hello 123"))
|
testIsSame(vm, t, "util.hex(util.md5('hello 123'))", u.MD5String("hello 123"))
|
||||||
testIsSame(vm, t, "util.sha1('hello 123')", u.Sha1([]byte("hello 123")))
|
testIsSame(vm, t, "util.sha1('hello',' 123')", u.Sha1([]byte("hello 123")))
|
||||||
testIsSame(vm, t, "util.sha256('hello 123')", u.Sha256([]byte("hello 123")))
|
testIsSame(vm, t, "util.sha256('hello 123')", u.Sha256([]byte("hello 123")))
|
||||||
testIsSame(vm, t, "util.sha512('hello 123')", u.Sha512([]byte("hello 123")))
|
testIsSame(vm, t, "util.sha512('hello 123')", u.Sha512([]byte("hello 123")))
|
||||||
testIsSame(vm, t, "util.json('hello 123')", u.Json("hello 123"))
|
testIsSame(vm, t, "util.json('hello 123')", u.Json("hello 123"))
|
||||||
|
Loading…
Reference in New Issue
Block a user