Skip to content

Commit 838b768

Browse files
authored
Buffer validation and optimized access to algorithm name
1 parent 309794e commit 838b768

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

HashifyNETCLI/Program.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -950,12 +950,14 @@ public static int Main(string[] args)
950950
{
951951
try
952952
{
953+
string hashFunctionName = GetHashFunctionName(fvar);
954+
953955
if (inputStream != null)
954956
{
955957
// Rewind the stream to the beginning for every algorithm to process the entire input.
956958
if (inputStream.Seek(0, SeekOrigin.Begin) != 0)
957959
{
958-
Logger.Error($"Failed to rewind the stream to the beginning for read operation by '{GetHashFunctionName(fvar)}'.");
960+
Logger.Error($"Failed to rewind the stream to the beginning for read operation by '{hashFunctionName}'.");
959961
return 1;
960962
}
961963
}
@@ -975,7 +977,7 @@ public static int Main(string[] args)
975977
IHashConfigBase configProfile = profile.Create();
976978
if (configProfile == null)
977979
{
978-
Logger.Error($"Could not get the config profile instance for algorithm '{GetHashFunctionName(fvar)}'.");
980+
Logger.Error($"Could not get the config profile instance for algorithm '{hashFunctionName}'.");
979981
PrintAlgorithms();
980982
return 1;
981983
}
@@ -1014,13 +1016,13 @@ public static int Main(string[] args)
10141016
}
10151017
catch (Exception ex)
10161018
{
1017-
Logger.Error("Could not create hash function instance: {0}", ex);
1019+
Logger.Error($"Could not create hash function instance for '{hashFunctionName}': {ex}");
10181020
return 1;
10191021
}
10201022

10211023
if (function == null)
10221024
{
1023-
Logger.Error("Failed to create hash function instance.");
1025+
Logger.Error($"Failed to create hash function instance for '{hashFunctionName}'.");
10241026
return 1;
10251027
}
10261028

@@ -1039,15 +1041,21 @@ public static int Main(string[] args)
10391041
if (inputStream.Length < chunkSize)
10401042
{
10411043
var buffer = new byte[inputStream.Length];
1042-
inputStream.Read(buffer, 0, buffer.Length);
1044+
1045+
if (inputStream.Read(buffer, 0, buffer.Length) != buffer.Length)
1046+
{
1047+
Logger.Error($"Failed to read the entire stream for '{hashFunctionName}'.");
1048+
return 1;
1049+
}
1050+
10431051
result = function.ComputeHash(buffer);
10441052
}
10451053
else
10461054
{
10471055
IBlockTransformer transformer = streamableHash.CreateBlockTransformer();
10481056
if (transformer == null)
10491057
{
1050-
Logger.Error($"Failed to create a valid block transformer for '{GetHashFunctionName(fvar)}'.");
1058+
Logger.Error($"Failed to create a valid block transformer for '{hashFunctionName}'.");
10511059
return 1;
10521060
}
10531061

@@ -1074,7 +1082,7 @@ public static int Main(string[] args)
10741082

10751083
Logger.LogDirect("\r", null, null);
10761084

1077-
Logger.LogDirect($"[{DateTimeOffset.UtcNow:MM/dd/yy-HH:mm:ss}] [{GetHashFunctionName(fvar)}] Transforming bytes: {totalRead}/{inputStream.Length} [", ConsoleColor.Gray, null);
1085+
Logger.LogDirect($"[{DateTimeOffset.UtcNow:MM/dd/yy-HH:mm:ss}] [{hashFunctionName}] Transforming bytes: {totalRead}/{inputStream.Length} [", ConsoleColor.Gray, null);
10781086

10791087
if (redBlocks > 0) Logger.LogDirect(new string('█', redBlocks), ConsoleColor.Red, null);
10801088
if (yellowBlocks > 0) Logger.LogDirect(new string('█', yellowBlocks), ConsoleColor.DarkYellow, null);
@@ -1088,21 +1096,31 @@ public static int Main(string[] args)
10881096

10891097
Logger.LogDirect("\n", null, null);
10901098

1099+
if (totalRead != inputStream.Length)
1100+
{
1101+
Logger.Error($"Failed to read the entire stream for '{hashFunctionName}'.");
1102+
return 1;
1103+
}
1104+
10911105
result = transformer.FinalizeHashValue();
10921106
}
10931107
}
10941108
else
10951109
{
10961110
if (inputStream.Length > int.MaxValue)
10971111
{
1098-
Logger.Error($"Unable to compute hash for '{GetHashFunctionName(fvar)}' with a stream size of over ~2 GB ({inputStream.Length} bytes). The all in once computation is designed for inputs smaller than 2 GB. Please pick algorithms that supports streaming data.");
1112+
Logger.Error($"Unable to compute hash for '{hashFunctionName}' with a stream size of over ~2 GB ({inputStream.Length} bytes). The all in once computation is designed for inputs smaller than 2 GB. Please pick algorithms that supports streaming data.");
10991113
return 1;
11001114
}
11011115

1102-
Logger.Warning($"Got a stream but the input algorithm '{GetHashFunctionName(fvar)}' does not support streaming computation. Trying to compute all data at once...");
1116+
Logger.Warning($"Got a stream but the input algorithm '{hashFunctionName}' does not support streaming computation. Trying to compute all data at once...");
11031117

11041118
byte[] buffer = new byte[inputStream.Length];
1105-
inputStream.Read(buffer, 0, buffer.Length);
1119+
if (inputStream.Read(buffer, 0, buffer.Length) != buffer.Length)
1120+
{
1121+
Logger.Error($"Failed to read the entire stream for '{hashFunctionName}'.");
1122+
return 1;
1123+
}
11061124

11071125
result = function.ComputeHash(buffer);
11081126
}
@@ -1120,7 +1138,7 @@ public static int Main(string[] args)
11201138

11211139
if (result == null)
11221140
{
1123-
Logger.Error($"Hash computation for '{GetHashFunctionName(fvar)}' returned a null result.");
1141+
Logger.Error($"Hash computation for '{hashFunctionName}' returned a null result.");
11241142
return 1;
11251143
}
11261144

@@ -1148,7 +1166,7 @@ public static int Main(string[] args)
11481166

11491167
try
11501168
{
1151-
scriptEngine.OutputScript(outputScript, finalizedOutput, GetHashFunctionName(fvar));
1169+
scriptEngine.OutputScript(outputScript, finalizedOutput, hashFunctionName);
11521170
}
11531171
catch (FailException ex)
11541172
{

0 commit comments

Comments
 (0)