2018-09-22 02:36:33 +02:00
|
|
|
package strings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/base64"
|
2018-10-14 19:06:27 +02:00
|
|
|
"net/url"
|
|
|
|
|
2018-09-22 02:36:33 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// EncodeURIComponent returns the encoded String of uri.
|
|
|
|
// @param (String) - Uri to encode.
|
|
|
|
// @returns String - Encoded string.
|
2018-09-22 02:36:33 +02:00
|
|
|
func EncodeURIComponent(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 1)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
str := url.QueryEscape(args[0].String())
|
|
|
|
|
|
|
|
return values.NewString(str), nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// Md5 calculates the MD5 checksum for text and return it in a hexadecimal string representation.
|
|
|
|
// @param text (String) - The string to do calculations against to.
|
|
|
|
// @return (String) - MD5 checksum as hex string.
|
2018-09-22 02:36:33 +02:00
|
|
|
func Md5(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 1)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
res := md5.Sum([]byte(text))
|
|
|
|
|
|
|
|
return values.NewString(string(res[:])), nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// Sha1 calculates the SHA1 checksum for text and returns it in a hexadecimal string representation.
|
|
|
|
// @param text (String) - The string to do calculations against to.
|
|
|
|
// @return (String) - Sha1 checksum as hex string.
|
2018-09-22 02:36:33 +02:00
|
|
|
func Sha1(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 1)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
res := sha1.Sum([]byte(text))
|
|
|
|
|
|
|
|
return values.NewString(string(res[:])), nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// Sha512 calculates the SHA512 checksum for text and returns it in a hexadecimal string representation.
|
|
|
|
// @param text (String) - The string to do calculations against to.
|
|
|
|
// @return (String) - SHA512 checksum as hex string.
|
2018-09-22 02:36:33 +02:00
|
|
|
func Sha512(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 1)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
res := sha512.Sum512([]byte(text))
|
|
|
|
|
|
|
|
return values.NewString(string(res[:])), nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// ToBase64 returns the base64 representation of value.
|
|
|
|
// @param value (string) - The string to encode.
|
|
|
|
// @returns toBase64String (String) - A base64 representation of the string.
|
2018-09-22 02:36:33 +02:00
|
|
|
func ToBase64(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 1)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := args[0].String()
|
|
|
|
out := base64.StdEncoding.EncodeToString([]byte(value))
|
|
|
|
|
|
|
|
return values.NewString(string(out)), nil
|
|
|
|
}
|