feat: add more crypto functions (#699)

* feat: add more crypto functions
This commit is contained in:
Rohit Jangid 2023-06-13 21:48:02 +05:30 committed by GitHub
parent 1096691eec
commit aece6e9b90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 109 additions and 15 deletions

View File

@ -33,6 +33,7 @@ go_library(
"//internal/parser:go_default_library",
"//internal/program:go_default_library",
"@io_k8s_sigs_yaml//:go_default_library",
"@org_golang_x_crypto//sha3:go_default_library",
],
)

View File

@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
@ -13,3 +13,9 @@ go_library(
importpath = "github.com/google/go-jsonnet/ast",
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["util_test.go"],
embed = [":go_default_library"],
)

View File

@ -21,10 +21,10 @@ def jsonnet_go_dependencies(go_sdk_version = "host"):
)
go_repository(
name = "com_github_fatih_color",
build_external = "external",
importpath = "github.com/fatih/color",
sum = "h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=",
version = "v1.12.0",
build_external = "external",
)
go_repository(
@ -47,17 +47,17 @@ def jsonnet_go_dependencies(go_sdk_version = "host"):
)
go_repository(
name = "com_github_mattn_go_colorable",
build_external = "external",
importpath = "github.com/mattn/go-colorable",
sum = "h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=",
version = "v0.1.8",
build_external = "external",
)
go_repository(
name = "com_github_mattn_go_isatty",
build_external = "external",
importpath = "github.com/mattn/go-isatty",
sum = "h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=",
version = "v0.0.12",
build_external = "external",
)
go_repository(
name = "com_github_pmezard_go_difflib",
@ -101,9 +101,34 @@ def jsonnet_go_dependencies(go_sdk_version = "host"):
sum = "h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=",
version = "v1.1.0",
)
go_repository(
name = "org_golang_x_crypto",
importpath = "golang.org/x/crypto",
sum = "h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=",
version = "v0.9.0",
)
go_repository(
name = "org_golang_x_net",
importpath = "golang.org/x/net",
sum = "h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=",
version = "v0.10.0",
)
go_repository(
name = "org_golang_x_sys",
importpath = "golang.org/x/sys",
sum = "h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=",
version = "v0.1.0",
sum = "h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=",
version = "v0.8.0",
)
go_repository(
name = "org_golang_x_term",
importpath = "golang.org/x/term",
sum = "h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=",
version = "v0.8.0",
)
go_repository(
name = "org_golang_x_text",
importpath = "golang.org/x/text",
sum = "h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=",
version = "v0.9.0",
)

View File

@ -19,6 +19,9 @@ package jsonnet
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"encoding/json"
@ -31,6 +34,7 @@ import (
"strings"
"github.com/google/go-jsonnet/ast"
"golang.org/x/crypto/sha3"
)
func builtinPlus(i *interpreter, x, y value) (value, error) {
@ -916,6 +920,42 @@ func builtinMd5(i *interpreter, x value) (value, error) {
return makeValueString(hex.EncodeToString(hash[:])), nil
}
func builtinSha1(i *interpreter, x value) (value, error) {
str, err := i.getString(x)
if err != nil {
return nil, err
}
hash := sha1.Sum([]byte(str.getGoString()))
return makeValueString(hex.EncodeToString(hash[:])), nil
}
func builtinSha256(i *interpreter, x value) (value, error) {
str, err := i.getString(x)
if err != nil {
return nil, err
}
hash := sha256.Sum256([]byte(str.getGoString()))
return makeValueString(hex.EncodeToString(hash[:])), nil
}
func builtinSha512(i *interpreter, x value) (value, error) {
str, err := i.getString(x)
if err != nil {
return nil, err
}
hash := sha512.Sum512([]byte(str.getGoString()))
return makeValueString(hex.EncodeToString(hash[:])), nil
}
func builtinSha3(i *interpreter, x value) (value, error) {
str, err := i.getString(x)
if err != nil {
return nil, err
}
hash := sha3.Sum512([]byte(str.getGoString()))
return makeValueString(hex.EncodeToString(hash[:])), nil
}
func builtinBase64(i *interpreter, input value) (value, error) {
var byteArr []byte
@ -2406,6 +2446,10 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&binaryBuiltin{name: "pow", function: builtinPow, params: ast.Identifiers{"x", "n"}},
&binaryBuiltin{name: "modulo", function: builtinModulo, params: ast.Identifiers{"x", "y"}},
&unaryBuiltin{name: "md5", function: builtinMd5, params: ast.Identifiers{"s"}},
&unaryBuiltin{name: "sha1", function: builtinSha1, params: ast.Identifiers{"s"}},
&unaryBuiltin{name: "sha256", function: builtinSha256, params: ast.Identifiers{"s"}},
&unaryBuiltin{name: "sha512", function: builtinSha512, params: ast.Identifiers{"s"}},
&unaryBuiltin{name: "sha3", function: builtinSha3, params: ast.Identifiers{"s"}},
&binaryBuiltin{name: "xnor", function: builtinXnor, params: ast.Identifiers{"x", "y"}},
&binaryBuiltin{name: "xor", function: builtinXor, params: ast.Identifiers{"x", "y"}},
&binaryBuiltin{name: "lstripChars", function: builtinLstripChars, params: ast.Identifiers{"str", "chars"}},

View File

@ -22,7 +22,7 @@ go_library(
],
cgo = True,
copts = ["-Wall -Icpp-jsonnet/include"], # keep
cxxopts = ["-std=c++11"],
cxxopts = ["-std=c++11 -Wall -Icpp-jsonnet/include"],
importpath = "github.com/google/go-jsonnet/c-bindings",
visibility = ["//visibility:private"],
deps = [

View File

@ -2,15 +2,16 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
go_library(
name = "go_default_library",
srcs = [
"main.go",
],
srcs = ["main.go"],
importpath = "github.com/google/go-jsonnet/cmd/wasm",
visibility = ["//visibility:private"],
deps = [
deps = select({
"@io_bazel_rules_go//go/platform:js_wasm": [
"//:go_default_library",
"//internal/formatter:go_default_library",
],
"//conditions:default": [],
}),
)
go_binary(

3
go.mod
View File

@ -11,6 +11,7 @@ require (
require (
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sys v0.8.0 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
)

4
go.sum
View File

@ -19,10 +19,14 @@ github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -169,6 +169,10 @@ func prepareStdlib(g *typeGraph) {
"base64DecodeBytes": g.newSimpleFuncType(numberType, "str"),
"base64Decode": g.newSimpleFuncType(stringType, "str"),
"md5": g.newSimpleFuncType(stringType, "s"),
"sha1": g.newSimpleFuncType(stringType, "s"),
"sha256": g.newSimpleFuncType(stringType, "s"),
"sha512": g.newSimpleFuncType(stringType, "s"),
"sha3": g.newSimpleFuncType(stringType, "s"),
// JSON Merge Patch

1
testdata/builtinSha1.golden vendored Normal file
View File

@ -0,0 +1 @@
"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"

1
testdata/builtinSha1.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
std.sha1("foo")

0
testdata/builtinSha1.linter.golden vendored Normal file
View File

1
testdata/builtinSha256.golden vendored Normal file
View File

@ -0,0 +1 @@
"2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"

1
testdata/builtinSha256.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
std.sha256("foo")

0
testdata/builtinSha256.linter.golden vendored Normal file
View File

1
testdata/builtinSha3.golden vendored Normal file
View File

@ -0,0 +1 @@
"4bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c82cbebc68e3b70a2a1480b4bb5d437a7cba6ecf9d89f9ff3ccd14cd6146ea7e7"

1
testdata/builtinSha3.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
std.sha3("foo")

0
testdata/builtinSha3.linter.golden vendored Normal file
View File

1
testdata/builtinSha512.golden vendored Normal file
View File

@ -0,0 +1 @@
"f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7"

1
testdata/builtinSha512.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
std.sha512("foo")

0
testdata/builtinSha512.linter.golden vendored Normal file
View File