github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/encode.go (about)

     1  package strings
     2  
     3  import (
     4  	"context"
     5  	"crypto/md5"
     6  	"crypto/sha1"
     7  	"crypto/sha512"
     8  	"encoding/base64"
     9  	"net/url"
    10  
    11  	"github.com/MontFerret/ferret/pkg/runtime/core"
    12  	"github.com/MontFerret/ferret/pkg/runtime/values"
    13  )
    14  
    15  // ENCODE_URI_COMPONENT returns the encoded String of uri.
    16  // @param {String} uri - Uri to encode.
    17  // @return {String} - Encoded string.
    18  func EncodeURIComponent(_ context.Context, args ...core.Value) (core.Value, error) {
    19  	err := core.ValidateArgs(args, 1, 1)
    20  
    21  	if err != nil {
    22  		return values.EmptyString, err
    23  	}
    24  
    25  	str := url.QueryEscape(args[0].String())
    26  
    27  	return values.NewString(str), nil
    28  }
    29  
    30  // MD5 calculates the MD5 checksum for text and return it in a hexadecimal string representation.
    31  // @param {String} str - The string to do calculations against to.
    32  // @return {String} - MD5 checksum as hex string.
    33  func Md5(_ context.Context, args ...core.Value) (core.Value, error) {
    34  	err := core.ValidateArgs(args, 1, 1)
    35  
    36  	if err != nil {
    37  		return values.EmptyString, err
    38  	}
    39  
    40  	text := args[0].String()
    41  	res := md5.Sum([]byte(text))
    42  
    43  	return values.NewString(string(res[:])), nil
    44  }
    45  
    46  // SHA1 calculates the SHA1 checksum for text and returns it in a hexadecimal string representation.
    47  // @param {String} str - The string to do calculations against to.
    48  // @return {String} - Sha1 checksum as hex string.
    49  func Sha1(_ context.Context, args ...core.Value) (core.Value, error) {
    50  	err := core.ValidateArgs(args, 1, 1)
    51  
    52  	if err != nil {
    53  		return values.EmptyString, err
    54  	}
    55  
    56  	text := args[0].String()
    57  	res := sha1.Sum([]byte(text))
    58  
    59  	return values.NewString(string(res[:])), nil
    60  }
    61  
    62  // SHA512 calculates the SHA512 checksum for text and returns it in a hexadecimal string representation.
    63  // @param {String} str - The string to do calculations against to.
    64  // @return {String} - SHA512 checksum as hex string.
    65  func Sha512(_ context.Context, args ...core.Value) (core.Value, error) {
    66  	err := core.ValidateArgs(args, 1, 1)
    67  
    68  	if err != nil {
    69  		return values.EmptyString, err
    70  	}
    71  
    72  	text := args[0].String()
    73  	res := sha512.Sum512([]byte(text))
    74  
    75  	return values.NewString(string(res[:])), nil
    76  }
    77  
    78  // TO_BASE64 returns the base64 representation of value.
    79  // @param {String} str - The string to encode.
    80  // @return {String} - A base64 representation of the string.
    81  func ToBase64(_ context.Context, args ...core.Value) (core.Value, error) {
    82  	err := core.ValidateArgs(args, 1, 1)
    83  
    84  	if err != nil {
    85  		return values.EmptyString, err
    86  	}
    87  
    88  	value := args[0].String()
    89  	out := base64.StdEncoding.EncodeToString([]byte(value))
    90  
    91  	return values.NewString(out), nil
    92  }