support multi input for hash

This commit is contained in:
Star 2024-10-26 12:08:13 +08:00
parent cc8fce2708
commit 991b21616d
4 changed files with 67 additions and 47 deletions

2
go.mod
View File

@ -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
View File

@ -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)

82
util.ts
View File

@ -43,44 +43,44 @@ export default {
isLocalPath isLocalPath
} }
function json(data:any): string {return ''} function json(data: any): string { return '' }
function jsonP(data:any): string {return ''} function jsonP(data: any): string { return '' }
function unJson(data:string): any {return null} function unJson(data: string): any { return null }
function yaml(data:any): string {return ''} function yaml(data: any): string { return '' }
function unYaml(data:string): any {return null} function unYaml(data: string): any { return null }
function load(filename:string): any {return null} function load(filename: string): any { return null }
function save(filename:string, data:any) {} function save(filename: string, data: any) { }
function base64(data:any): string {return ''} function base64(data: any): string { return '' }
function unBase64(data:string): any {return null} function unBase64(data: string): any { return null }
function urlBase64(data:any): string {return ''} function urlBase64(data: any): string { return '' }
function unUrlBase64(data:string): any {return null} function unUrlBase64(data: string): any { return null }
function hex(data:any): string {return ''} function hex(data: any): string { return '' }
function unHex(data:string): any {return null} function unHex(data: string): any { return null }
function aes(data:any, key:string, iv:string): string {return ''} function aes(data: any, key: string, iv: string): string { return '' }
function unAes(data:string, key:string, iv:string): any {return null} function unAes(data: string, key: string, iv: string): any { return null }
function gzip(data:any): string {return ''} function gzip(data: any): string { return '' }
function gunzip(data:string): any {return null} 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 { }
function shell(cmd:string, ...args:string[]): string[] {return []} function shell(cmd: string, ...args: string[]): string[] { return [] }
function toDatetime(timestamp:number): string {return ''} function toDatetime(timestamp: number): string { return '' }
function fromDatetime(datetimeStr:string): number {return 0} function fromDatetime(datetimeStr: string): number { return 0 }
function toDate(timestamp:number): string {return ''} function toDate(timestamp: number): string { return '' }
function fromDate(dateStr:string): number {return 0} function fromDate(dateStr: string): number { return 0 }
function os(): string {return ''} function os(): string { return '' }
function arch(): string {return ''} function arch(): string { return '' }
function joinPath(...paths:string[]): string {return ''} function joinPath(...paths: string[]): string { return '' }
function getPathDir(path:string): string {return ''} function getPathDir(path: string): string { return '' }
function getPathBase(path:string): string {return ''} function getPathBase(path: string): string { return '' }
function getPathVolume(path:string): string {return ''} function getPathVolume(path: string): string { return '' }
function absPath(path:string): string {return ''} function absPath(path: string): string { return '' }
function cleanPath(path:string): string {return ''} function cleanPath(path: string): string { return '' }
function isLocalPath(path:string): boolean {return false} function isLocalPath(path: string): boolean { return false }

View File

@ -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"))