/
crypto_encrypt_base64.go
68 lines (56 loc) · 2.12 KB
/
crypto_encrypt_base64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Code generated by __generator__/interpreter.go at once
package builtin
import (
"encoding/base64"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/function/errors"
"github.com/ysugimoto/falco/interpreter/function/shared"
"github.com/ysugimoto/falco/interpreter/value"
)
const Crypto_encrypt_base64_Name = "crypto.encrypt_base64"
var Crypto_encrypt_base64_ArgumentTypes = []value.Type{value.IdentType, value.IdentType, value.IdentType, value.StringType, value.StringType, value.StringType}
func Crypto_encrypt_base64_Validate(args []value.Value) error {
if len(args) != 6 {
return errors.ArgumentNotEnough(Crypto_encrypt_base64_Name, 6, args)
}
for i := range args {
if args[i].Type() != Crypto_encrypt_base64_ArgumentTypes[i] {
return errors.TypeMismatch(Crypto_encrypt_base64_Name, i+1, Crypto_encrypt_base64_ArgumentTypes[i], args[i].Type())
}
}
return nil
}
// Fastly built-in function implementation of crypto.encrypt_base64
// Arguments may be:
// - ID, ID, ID, STRING, STRING, STRING
// Reference: https://developer.fastly.com/reference/vcl/functions/cryptographic/crypto-encrypt-base64/
func Crypto_encrypt_base64(ctx *context.Context, args ...value.Value) (value.Value, error) {
// Argument validations
if err := Crypto_encrypt_base64_Validate(args); err != nil {
return value.Null, err
}
cipherId := value.Unwrap[*value.Ident](args[0])
mode := value.Unwrap[*value.Ident](args[1])
padding := value.Unwrap[*value.Ident](args[2])
key := value.Unwrap[*value.String](args[3])
iv := value.Unwrap[*value.String](args[4])
text := value.Unwrap[*value.String](args[5])
buf, err := base64.StdEncoding.DecodeString(text.Value)
if err != nil {
return value.Null, errors.New("Failed to decode base64 encoded string: %s", text.Value)
}
codec, err := shared.NewCryptoCodec(
Crypto_encrypt_base64_Name,
cipherId.Value, mode.Value, padding.Value,
)
if err != nil {
return value.Null, err
}
encrypted, err := codec.Encrypt(key.Value, iv.Value, buf)
if err != nil {
return value.Null, err
}
return &value.String{
Value: base64.StdEncoding.EncodeToString(encrypted),
}, nil
}