From 991b21616d0d42effe6732b33904bfb6aeb8cbc7 Mon Sep 17 00:00:00 2001 From: Star Date: Sat, 26 Oct 2024 12:08:13 +0800 Subject: [PATCH] support multi input for hash --- go.mod | 2 +- util.go | 28 +++++++++++++++--- util.ts | 82 ++++++++++++++++++++++++++-------------------------- util_test.go | 2 +- 4 files changed, 67 insertions(+), 47 deletions(-) diff --git a/go.mod b/go.mod index 716d0fb..7250c62 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module apigo.cc/gojs/util go 1.18 require ( - apigo.cc/gojs v0.0.1 + apigo.cc/gojs v0.0.3 github.com/ssgo/u v1.7.9 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/util.go b/util.go index f3b04b1..a25533f 100644 --- a/util.go +++ b/util.go @@ -2,6 +2,10 @@ package util import ( "bytes" + "crypto/md5" + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" _ "embed" "encoding/hex" "encoding/json" @@ -185,19 +189,35 @@ func init() { }, "md5": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { 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 { 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 { 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 { 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 { args := gojs.MakeArgs(&argsIn, vm).Check(2) diff --git a/util.ts b/util.ts index 5a0f2f7..4c136d4 100644 --- a/util.ts +++ b/util.ts @@ -43,44 +43,44 @@ export default { isLocalPath } -function json(data:any): string {return ''} -function jsonP(data:any): string {return ''} -function unJson(data:string): any {return null} -function yaml(data:any): string {return ''} -function unYaml(data:string): any {return null} -function load(filename:string): any {return null} -function save(filename:string, data:any) {} -function base64(data:any): string {return ''} -function unBase64(data:string): any {return null} -function urlBase64(data:any): string {return ''} -function unUrlBase64(data:string): any {return null} -function hex(data:any): string {return ''} -function unHex(data:string): any {return null} -function aes(data:any, key:string, iv:string): string {return ''} -function unAes(data:string, key:string, iv:string): any {return null} -function gzip(data:any): string {return ''} -function gunzip(data:string): any {return null} -function id(): string {return ''} -function uniqueId(): string {return ''} -function token(size:number): string {return ''} -function md5(data:any): string {return ''} -function sha1(data:any): string {return ''} -function sha256(data:any): string {return ''} -function sha512(data:any): string {return ''} -function tpl(text:string, data:any, functions?:Object): string {return ''} -function sleep(ms:number): void {} -function setTimeout(callback:()=>void, ms?:number, ...args:any): void {} -function shell(cmd:string, ...args:string[]): string[] {return []} -function toDatetime(timestamp:number): string {return ''} -function fromDatetime(datetimeStr:string): number {return 0} -function toDate(timestamp:number): string {return ''} -function fromDate(dateStr:string): number {return 0} -function os(): string {return ''} -function arch(): string {return ''} -function joinPath(...paths:string[]): string {return ''} -function getPathDir(path:string): string {return ''} -function getPathBase(path:string): string {return ''} -function getPathVolume(path:string): string {return ''} -function absPath(path:string): string {return ''} -function cleanPath(path:string): string {return ''} -function isLocalPath(path:string): boolean {return false} +function json(data: any): string { return '' } +function jsonP(data: any): string { return '' } +function unJson(data: string): any { return null } +function yaml(data: any): string { return '' } +function unYaml(data: string): any { return null } +function load(filename: string): any { return null } +function save(filename: string, data: any) { } +function base64(data: any): string { return '' } +function unBase64(data: string): any { return null } +function urlBase64(data: any): string { return '' } +function unUrlBase64(data: string): any { return null } +function hex(data: any): string { return '' } +function unHex(data: string): any { return null } +function aes(data: any, key: string, iv: string): string { return '' } +function unAes(data: string, key: string, iv: string): any { return null } +function gzip(data: any): string { return '' } +function gunzip(data: string): any { return null } +function id(): string { return '' } +function uniqueId(): string { return '' } +function token(size: number): string { return '' } +function md5(...data: any[]): string { return '' } +function sha1(...data: any[]): string { return '' } +function sha256(...data: any[]): string { return '' } +function sha512(...data: any[]): string { return '' } +function tpl(text: string, data: any, functions?: Object): string { return '' } +function sleep(ms: number): void { } +function setTimeout(callback: () => void, ms?: number, ...args: any): void { } +function shell(cmd: string, ...args: string[]): string[] { return [] } +function toDatetime(timestamp: number): string { return '' } +function fromDatetime(datetimeStr: string): number { return 0 } +function toDate(timestamp: number): string { return '' } +function fromDate(dateStr: string): number { return 0 } +function os(): string { return '' } +function arch(): string { return '' } +function joinPath(...paths: string[]): string { return '' } +function getPathDir(path: string): string { return '' } +function getPathBase(path: string): string { return '' } +function getPathVolume(path: string): string { return '' } +function absPath(path: string): string { return '' } +function cleanPath(path: string): string { return '' } +function isLocalPath(path: string): boolean { return false } diff --git a/util_test.go b/util_test.go index d295665..412735a 100644 --- a/util_test.go +++ b/util_test.go @@ -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.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.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.sha512('hello 123')", u.Sha512([]byte("hello 123"))) testIsSame(vm, t, "util.json('hello 123')", u.Json("hello 123"))