Added filebase64sha256 support for DotNet

This commit is contained in:
Vova Ivanov 2021-11-23 16:51:19 -08:00
parent 584f109588
commit 1c1e7e9d7d
2 changed files with 16 additions and 6 deletions

View file

@ -238,12 +238,13 @@ func (g *generator) genRange(w io.Writer, call *model.FunctionCallExpression, en
}
var functionNamespaces = map[string][]string{
"readDir": {"System.IO", "System.Linq"},
"readFile": {"System.IO"},
"filebase64": {"System", "System.IO"},
"toJSON": {"System.Text.Json", "System.Collections.Generic"},
"toBase64": {"System"},
"sha1": {"System.Security.Cryptography", "System.Text"},
"readDir": {"System.IO", "System.Linq"},
"readFile": {"System.IO"},
"filebase64": {"System", "System.IO"},
"filebase64sha256": {"System", "System.IO", "System.Security.Cryptography", "System.Text"},
"toJSON": {"System.Text.Json", "System.Collections.Generic"},
"toBase64": {"System"},
"sha1": {"System.Security.Cryptography", "System.Text"},
}
func (g *generator) genFunctionUsings(x *model.FunctionCallExpression) []string {
@ -313,6 +314,9 @@ func (g *generator) GenFunctionCallExpression(w io.Writer, expr *model.FunctionC
case "filebase64":
// Assuming the existence of the following helper method located earlier in the preamble
g.Fgenf(w, "ReadFileBase64(%v)", expr.Args[0])
case "filebase64sha256":
// Assuming the existence of the following helper method located earlier in the preamble
g.Fgenf(w, "ComputeFileBase64Sha256(%v)", expr.Args[0])
case pcl.Invoke:
_, name := g.functionName(expr.Args[0])

View file

@ -128,6 +128,12 @@ func getHelperMethodIfNeeded(functionName string) (string, bool) {
return `private static string ReadFileBase64(string path) {
return Convert.ToBase64String(System.Text.UTF8.GetBytes(File.ReadAllText(path)))
}`, true
case "filebase64sha256":
return `private static string ComputeFileBase64Sha256(string path) {
var fileData = System.Text.UTF8.GetBytes(File.ReadAllText(path))
var hashData = SHA256.Create().ComputeHash(fileData)
return Convert.ToBase64String(BitConverter.ToString(hashData).Replace("-","").ToLowerInvariant())
}`, true
case "sha1":
return `private static string ComputeSHA1(string input) {
return BitConverter.ToString(