Skip to content

Commit 1849954

Browse files
authored
Add Suffix Support
1 parent 621aee0 commit 1849954

File tree

1 file changed

+70
-50
lines changed

1 file changed

+70
-50
lines changed

HashifyNETCLI/Program.cs

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -116,56 +116,82 @@ static void PrintUsage()
116116
Logger.LogDirect("\n", ConsoleColor.DarkRed, null);
117117
}
118118

119-
static string GetHashFunctionName(Type type)
119+
static string GetHashFunctionName(FunctionVar fvar, bool noVar = false)
120120
{
121-
if (!type.IsInterface)
121+
if (!fvar.Function.IsInterface)
122122
return "N/A";
123123

124-
return type.Name.Remove(0, 1);
124+
if (fvar.Name == null || noVar)
125+
return fvar.Function.Name.Remove(0, 1);
126+
127+
return fvar.Function.Name.Remove(0, 1) + ":" + fvar.Name;
125128
}
126129

127130
static string GetHashFunctionOriginalName(string name)
128131
{
129132
return "I" + name;
130133
}
131134

132-
static IReadOnlyList<Type> GetHashFunction(string name)
135+
static IReadOnlyList<FunctionVar> GetHashFunction(string query)
133136
{
134-
List<Type> types = new List<Type>();
135-
string interfaceName = GetHashFunctionOriginalName(name);
137+
List<FunctionVar> types = new List<FunctionVar>();
136138
Type[] cryptographicHashes = HashFactory.GetHashAlgorithms(HashFunctionType.Cryptographic);
137139
Type[] noncryptographicHashes = HashFactory.GetHashAlgorithms(HashFunctionType.Noncryptographic);
138140

139-
if (name == "*")
141+
if (query == "*")
140142
{
141-
types.AddRange(cryptographicHashes);
142-
types.AddRange(noncryptographicHashes);
143+
IEnumerable<Type> h = cryptographicHashes.Union(noncryptographicHashes);
144+
foreach (Type t in h)
145+
{
146+
types.Add(new FunctionVar(null, t));
147+
}
148+
143149
return types;
144-
}
150+
}
145151

146-
string[] n = name.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
152+
string[] n = query.Split(' ', StringSplitOptions.RemoveEmptyEntries);
147153
if (n == null || n.Length < 1)
148154
{
149155
return types;
150156
}
151157

152158
foreach (string s in n)
153159
{
154-
foreach (Type t1 in cryptographicHashes)
160+
string actualName;
161+
string? suffix = null;
162+
if (s.Contains(":"))
155163
{
156-
if (t1.Name.Equals(interfaceName, StringComparison.OrdinalIgnoreCase))
164+
string[] p = s.Split(':', StringSplitOptions.RemoveEmptyEntries);
165+
if (p.Length != 2)
157166
{
158-
types.Add(t1);
167+
// Error, just clear and return an empty result.
168+
types.Clear();
169+
return types;
170+
}
171+
172+
actualName = GetHashFunctionOriginalName(p[0].Trim());
173+
suffix = p[1].Trim();
174+
}
175+
else
176+
{
177+
actualName = GetHashFunctionOriginalName(s);
178+
}
179+
180+
foreach (Type t1 in cryptographicHashes)
181+
{
182+
if (t1.Name.Equals(actualName, StringComparison.OrdinalIgnoreCase))
183+
{
184+
types.Add(new FunctionVar(suffix, t1));
159185
break;
160186
}
161187
}
162188

163189
foreach (Type t2 in noncryptographicHashes)
164190
{
165-
if (t2.Name.Equals(interfaceName, StringComparison.OrdinalIgnoreCase))
191+
if (t2.Name.Equals(actualName, StringComparison.OrdinalIgnoreCase))
166192
{
167-
types.Add(t2);
168-
break;
193+
types.Add(new FunctionVar(suffix, t2));
194+
break;
169195
}
170196
}
171197
}
@@ -181,18 +207,18 @@ static void PrintAlgorithms()
181207

182208
foreach (Type type in cryptographicHashes)
183209
{
184-
Logger.Inform($" - {GetHashFunctionName(type)} (Cryptographic)");
210+
Logger.Inform($" - {GetHashFunctionName(new FunctionVar(null, type))} (Cryptographic)");
185211
}
186212

187213
foreach (Type type in noncryptographicHashes)
188214
{
189-
Logger.Inform($" - {GetHashFunctionName(type)} (Non-Cryptographic)");
215+
Logger.Inform($" - {GetHashFunctionName(new FunctionVar(null, type))} (Non-Cryptographic)");
190216
}
191217
}
192218

193219
static void PrintProfiles(Type algorithm)
194220
{
195-
string algoName = GetHashFunctionName(algorithm);
221+
string algoName = GetHashFunctionName(new FunctionVar(null, algorithm));
196222

197223
IHashConfigProfile[] profiles = HashFactory.GetConfigProfiles(algorithm);
198224
if (profiles.Length < 1)
@@ -241,9 +267,9 @@ static void PrintProfiles(Type algorithm)
241267
return retval;
242268
}
243269

244-
static IHashConfigProfile? GetConfigProfile(Type algorithm, string profileName)
270+
static IHashConfigProfile? GetConfigProfile(FunctionVar fvar, string profileName)
245271
{
246-
IHashConfigProfile[] profiles = HashFactory.GetConfigProfiles(algorithm);
272+
IHashConfigProfile[] profiles = HashFactory.GetConfigProfiles(fvar.Function);
247273
if (profiles.Length < 1)
248274
{
249275
return null;
@@ -260,14 +286,15 @@ static void PrintProfiles(Type algorithm)
260286
return null;
261287
}
262288

263-
static IHashConfigProfile? GetConfigProfile(Type algorithm, Dictionary<string, string> query)
289+
static IHashConfigProfile? GetConfigProfile(FunctionVar fvar, Dictionary<string, string> query)
264290
{
265-
string algoName = GetHashFunctionName(algorithm);
291+
string algoName = GetHashFunctionName(fvar);
292+
string algoName_novar = GetHashFunctionName(fvar, true);
266293

267-
string? profile = null;
294+
string? profile = null;
268295
foreach (KeyValuePair<string, string> pair in query)
269296
{
270-
if (algoName.Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
297+
if (algoName.Equals(pair.Key, StringComparison.OrdinalIgnoreCase) || algoName_novar.Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
271298
{
272299
profile = pair.Value;
273300
break;
@@ -279,7 +306,7 @@ static void PrintProfiles(Type algorithm)
279306
return null;
280307
}
281308

282-
IHashConfigProfile[] profiles = HashFactory.GetConfigProfiles(algorithm);
309+
IHashConfigProfile[] profiles = HashFactory.GetConfigProfiles(fvar.Function);
283310
if (profiles.Length < 1)
284311
{
285312
return null;
@@ -527,14 +554,14 @@ static void PrintProfiles(Type algorithm)
527554
continue;
528555
}
529556

530-
IReadOnlyList<Type> hashFunctionTypes = GetHashFunction(n);
557+
IReadOnlyList<FunctionVar> hashFunctionTypes = GetHashFunction(n);
531558
if (hashFunctionTypes.Count < 1)
532559
{
533560
Logger.Warning($"Could not find a hash algorithm type with the given JSON name '{n}'.");
534561
continue;
535562
}
536563

537-
Type hashFunctionType = hashFunctionTypes[0];
564+
FunctionVar hashFunctionType = hashFunctionTypes[0];
538565
var body = property.Value.EnumerateObject();
539566
JsonProperty? bodyFirstProp = null;
540567
foreach (var bodyProp in body)
@@ -577,7 +604,7 @@ static void PrintProfiles(Type algorithm)
577604
continue;
578605
}
579606

580-
retval.Add((hashFunctionType, configBase));
607+
retval.Add((hashFunctionType.Function, configBase));
581608
}
582609
else if (bodyFirstProp.Value.Name.Equals("config", StringComparison.OrdinalIgnoreCase))
583610
{
@@ -587,7 +614,7 @@ static void PrintProfiles(Type algorithm)
587614
continue;
588615
}
589616

590-
if (!HashFactory.TryCreateDefaultConcreteConfig(hashFunctionType, out IHashConfigBase config))
617+
if (!HashFactory.TryCreateDefaultConcreteConfig(hashFunctionType.Function, out IHashConfigBase config))
591618
{
592619
Logger.Warning($"Unable to create an instance of default concrete hash config for '{n}'.");
593620
continue;
@@ -641,7 +668,7 @@ static void PrintProfiles(Type algorithm)
641668
continue;
642669
}
643670

644-
retval.Add((hashFunctionType, config));
671+
retval.Add((hashFunctionType.Function, config));
645672
}
646673
}
647674

@@ -829,15 +856,15 @@ public static int Main(string[] args)
829856
string lprofiles = cl.GetValueString("-lp", null!);
830857
if (lprofiles != null)
831858
{
832-
IReadOnlyList<Type> algos = GetHashFunction(lprofiles);
859+
IReadOnlyList<FunctionVar> algos = GetHashFunction(lprofiles);
833860
if (algos == null || algos.Count < 1)
834861
{
835862
Logger.Error($"No algorithm found with '{lprofiles}' to retrieve profiles for.");
836863
PrintUsage();
837864
return 1;
838865
}
839866

840-
Type f = algos[0];
867+
Type f = algos[0].Function;
841868
PrintProfiles(f);
842869
return 0;
843870
}
@@ -965,29 +992,22 @@ public static int Main(string[] args)
965992
return 1;
966993
}
967994

968-
IReadOnlyList<Type> types = GetHashFunction(algorithm);
995+
IReadOnlyList<FunctionVar> types = GetHashFunction(algorithm);
969996
if (types == null || types.Count < 1)
970997
{
971998
Logger.Error("Invalid or unsupported algorithm specified.");
972999
PrintAlgorithms();
9731000
return 1;
9741001
}
9751002

976-
foreach (Type type in types)
1003+
foreach (FunctionVar fvar in types)
9771004
{
9781005
try
9791006
{
980-
if (type == null)
981-
{
982-
Logger.Error("Invalid or unsupported algorithm specified.");
983-
PrintAlgorithms();
984-
return 1;
985-
}
986-
9871007
IHashConfigProfile? profile = null;
9881008
if (configProfileQueries != null)
9891009
{
990-
profile = GetConfigProfile(type, configProfileQueries);
1010+
profile = GetConfigProfile(fvar, configProfileQueries);
9911011
}
9921012

9931013
IHashFunctionBase function;
@@ -999,28 +1019,28 @@ public static int Main(string[] args)
9991019
IHashConfigBase configProfile = profile.Create();
10001020
if (configProfile == null)
10011021
{
1002-
Logger.Error($"Could not get the config profile instance for algorithm '{GetHashFunctionName(type)}'.");
1022+
Logger.Error($"Could not get the config profile instance for algorithm '{GetHashFunctionName(fvar)}'.");
10031023
PrintAlgorithms();
10041024
return 1;
10051025
}
10061026

1007-
function = HashFactory.Create(type, configProfile);
1027+
function = HashFactory.Create(fvar.Function, configProfile);
10081028
}
10091029
else
10101030
{
10111031
(Type AlgorithmType, IHashConfigBase Config)? jsonConfig = null;
10121032
if (_jsonConfigs != null && _jsonConfigs.Count > 0)
10131033
{
1014-
jsonConfig = _jsonConfigs.Where(t => t.AlgorithmType == type).FirstOrDefault();
1034+
jsonConfig = _jsonConfigs.Where(t => t.AlgorithmType == fvar.Function).FirstOrDefault();
10151035
}
10161036

10171037
if (jsonConfig.HasValue)
10181038
{
1019-
function = HashFactory.Create(type, jsonConfig.Value.Config);
1039+
function = HashFactory.Create(fvar.Function, jsonConfig.Value.Config);
10201040
}
10211041
else
10221042
{
1023-
function = HashFactory.Create(type);
1043+
function = HashFactory.Create(fvar.Function);
10241044
}
10251045
}
10261046
}
@@ -1050,7 +1070,7 @@ public static int Main(string[] args)
10501070
object finalizedOutput = FinalizeOutputScript(outputFinalizer, result);
10511071
try
10521072
{
1053-
OutputScript(outputScript, finalizedOutput, GetHashFunctionName(type));
1073+
OutputScript(outputScript, finalizedOutput, GetHashFunctionName(fvar));
10541074
}
10551075
catch (Exception ex)
10561076
{

0 commit comments

Comments
 (0)