add new functions for scripts: sha256, sha512, generateUUIDv4

This commit is contained in:
Мержанов Максим Владимирович committed 2021-04-13 09:39:50 +00:00
1 parent 4f14ea38e1
commit 166529138f
5 files changed
+36 -51

No files matched your search

+30
View File
@@ -1,10 +1,14 @@
package service
import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/integration-system/isp-lib/v2/config"
"github.com/integration-system/isp-lib/v2/scripts"
log "github.com/integration-system/isp-log"
@@ -121,7 +125,14 @@ func (s *scriptService) executeScript(scr CompiledScript, arg interface{}) *doma
cfg := config.GetRemote().(*conf.RemoteConfig)
response, err := s.scriptEngine.Execute(scr.Script, arg,
scripts.WithScriptTimeout(time.Duration(cfg.ScriptExecutionTimeoutMs)*time.Millisecond),
// TODO: remove. invoke is deprecated, all functions should be inside `external` object
scripts.WithSet("invoke", router.Invoke),
scripts.WithSet("external", map[string]interface{}{
"invoke": router.Invoke,
"hashSha256": Sha256,
"hashSha512": Sha512,
"generateUUIDv4": UUIDv4,
}),
)
if err != nil {
return s.respError(err, domain.ErrorRunTime)
@@ -145,3 +156,22 @@ func (*scriptService) respError(err error, errorType string) *domain.ScriptResp
Error: &respError,
}
}
func Sha256(value string) string {
sum := sha256.Sum256([]byte(value))
return hex.EncodeToString(sum[:])
}
func Sha512(value string) string {
sum := sha512.Sum512([]byte(value))
return hex.EncodeToString(sum[:])
}
func UUIDv4() string {
randomUUID, err := uuid.NewRandom()
if err != nil {
// will be handled as exception in js
panic(err)
}
return randomUUID.String()
}