浏览代码

ISPP change: Added support functions GetSHA256OfFile, GetSHA256OfString, and GetSHA256OfUnicodeString. Also some cleanup.

Martijn Laan 1 年之前
父节点
当前提交
b1a0ff0161
共有 4 个文件被更改,包括 153 次插入43 次删除
  1. 52 3
      ISHelp/ispp.xml
  2. 88 25
      Projects/Src/ISPP.Funcs.pas
  3. 11 14
      Projects/Src/Setup.InstFunc.pas
  4. 2 1
      whatsnew.htm

+ 52 - 3
ISHelp/ispp.xml

@@ -1249,7 +1249,6 @@ The first group of options (<tt>option</tt>) controls the general options, while
 				</section>
 				<description>
 					<p>Gets the MD5 sum of the specified Unicode string, as a string.</p>
-					<p>Causes an internal error if called during non Unicode compilation.</p>
 				</description>
 				<section title="Example">
 					<pre>
@@ -1275,16 +1274,27 @@ The first group of options (<tt>option</tt>) controls the general options, while
 				<description>
 					<p>Gets the SHA-1 hash of the specified ANSI string, as a string.</p>
 				</description>
+				<section title="Example">
+					<pre>
+						<line>#define SHA1 GetSHA1OfString('Test')</line>
+						<line>// SHA1 = '640ab2bae07bedc4c163f679a746f7ab7fb5d1fa'</line>
+					</pre>
+				</section>
 			</topic>
 			<topic id="GetSHA1OfUnicodeString">
 				<title>GetSHA1OfUnicodeString</title>
 				<section title="Prototype">
-					<pre><line><b>str</b> GetSHA1OfString(<b>str</b>)</line></pre>
+					<pre><line><b>str</b> GetSHA1OfUnicodeString(<b>str</b>)</line></pre>
 				</section>
 				<description>
 					<p>Gets the SHA-1 hash of the specified Unicode string, as a string.</p>
-					<p>Causes an internal error if called during non Unicode compilation.</p>
 				</description>
+				<section title="Example">
+					<pre>
+						<line>#define SHA1 GetSHA1OfUnicodeString('Test')</line>
+						<line>// SHA1 = '9ab696a37604d665dc97134dbee44cfe70451b1a'</line>
+					</pre>
+				</section>
 			</topic>
 			<topic id="GetSHA1OfFile">
 				<title>GetSHA1OfFile</title>
@@ -1295,6 +1305,45 @@ The first group of options (<tt>option</tt>) controls the general options, while
 					<p>Gets the SHA-1 hash of the specified file, as a string.</p>
 				</description>
 			</topic>
+			<topic id="GetSHA256OfString">
+				<title>GetSHA256OfString</title>
+				<section title="Prototype">
+					<pre><line><b>str</b> GetSHA256OfString(<b>str</b>)</line></pre>
+				</section>
+				<description>
+					<p>Gets the SHA-256 hash of the specified ANSI string, as a string.</p>
+				</description>
+				<section title="Example">
+					<pre>
+						<line>#define SHA256 GetSHA256OfString('Test')</line>
+						<line>// SHA256 = '532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e25'</line>
+					</pre>
+				</section>
+			</topic>
+			<topic id="GetSHA256OfUnicodeString">
+				<title>GetSHA256OfUnicodeString</title>
+				<section title="Prototype">
+					<pre><line><b>str</b> GetSHA256OfString(<b>str</b>)</line></pre>
+				</section>
+				<description>
+					<p>Gets the SHA-256 hash of the specified Unicode string, as a string.</p>
+				</description>
+				<section title="Example">
+					<pre>
+						<line>#define SHA256 GetSHA256OfUnicodeString('Test')</line>
+						<line>// SHA256 = 'e6fa3ca87b1b641ab646d3b4933bba8d0970763f030b6578a60abdeae7366247'</line>
+					</pre>
+				</section>
+			</topic>
+			<topic id="GetSHA256OfFile">
+				<title>GetSHA256OfFile</title>
+				<section title="Prototype">
+					<pre><line><b>str</b> GetSHA256OfFile(<b>str</b>)</line></pre>
+				</section>
+				<description>
+					<p>Gets the SHA-256 hash of the specified file, as a string.</p>
+				</description>
+			</topic>
 			<topic id="Trim">
 				<title>Trim</title>
 				<section title="Prototype">

+ 88 - 25
Projects/Src/ISPP.Funcs.pas

@@ -22,7 +22,7 @@ implementation
 uses
   SysUtils, IniFiles, Registry, Math, ISPP.Consts, ISPP.Base, ISPP.IdentMan,
   ISPP.Sessions, DateUtils, Shared.FileClass, MD5, SHA1, PathFunc, Shared.CommonFunc,
-  Shared.Int64Em;
+  Shared.Int64Em, Hash;
   
 var
   IsWin64: Boolean;
@@ -1661,21 +1661,20 @@ end;
 function GetMD5OfFile(Ext: Longint; const Params: IIsppFuncParams;
   const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
 var
-  F: TFile;
-  NumRead: Cardinal;
-  Context: TMD5Context;
   Buf: array[0..65535] of Byte;
 begin
   if CheckParams(Params, [evStr], 1, Result) then
   try
     with IInternalFuncParams(Params) do
     begin
+      var Context: TMD5Context;
       MD5Init(Context);
-      F := TFile.Create(PrependPath(Ext, Get(0).AsStr), fdOpenExisting, faRead, fsReadWrite);
+      var F := TFile.Create(PrependPath(Ext, Get(0).AsStr), fdOpenExisting, faRead, fsReadWrite);
       try
         while True do begin
-          NumRead := F.Read(Buf, SizeOf(Buf));
-          if NumRead = 0 then Break;
+          var NumRead := F.Read(Buf, SizeOf(Buf));
+          if NumRead = 0 then
+            Break;
           MD5Update(Context, Buf, NumRead);
         end;
       finally
@@ -1694,14 +1693,12 @@ end;
 
 function GetMD5OfString(Ext: Longint; const Params: IIsppFuncParams;
   const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
-var
-  S: AnsiString;
 begin
   if CheckParams(Params, [evStr], 1, Result) then
   try
     with IInternalFuncParams(Params) do
     begin
-      S := AnsiString(Get(0).AsStr);
+      var S := AnsiString(Get(0).AsStr);
       MakeStr(ResPtr^, MD5DigestToString(MD5Buf(Pointer(S)^, Length(S)*SizeOf(S[1]))));
     end;
   except
@@ -1715,14 +1712,12 @@ end;
 
 function GetMD5OfUnicodeString(Ext: Longint; const Params: IIsppFuncParams;
   const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
-var
-  S: UnicodeString;
 begin
   if CheckParams(Params, [evStr], 1, Result) then
   try
     with IInternalFuncParams(Params) do
     begin
-      S := Get(0).AsStr;
+      var S := Get(0).AsStr;
       MakeStr(ResPtr^, MD5DigestToString(MD5Buf(Pointer(S)^, Length(S)*SizeOf(S[1]))));
     end;
   except
@@ -1737,21 +1732,20 @@ end;
 function GetSHA1OfFile(Ext: Longint; const Params: IIsppFuncParams;
   const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
 var
-  F: TFile;
-  NumRead: Cardinal;
-  Context: TSHA1Context;
   Buf: array[0..65535] of Byte;
 begin
   if CheckParams(Params, [evStr], 1, Result) then
   try
     with IInternalFuncParams(Params) do
     begin
+      var Context: TSHA1Context;
       SHA1Init(Context);
-      F := TFile.Create(PrependPath(Ext, Get(0).AsStr), fdOpenExisting, faRead, fsReadWrite);
+      var F := TFile.Create(PrependPath(Ext, Get(0).AsStr), fdOpenExisting, faRead, fsReadWrite);
       try
         while True do begin
-          NumRead := F.Read(Buf, SizeOf(Buf));
-          if NumRead = 0 then Break;
+          var NumRead := F.Read(Buf, SizeOf(Buf));
+          if NumRead = 0 then
+            Break;
           SHA1Update(Context, Buf, NumRead);
         end;
       finally
@@ -1770,14 +1764,12 @@ end;
 
 function GetSHA1OfString(Ext: Longint; const Params: IIsppFuncParams;
   const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
-var
-  S: AnsiString;
 begin
   if CheckParams(Params, [evStr], 1, Result) then
   try
     with IInternalFuncParams(Params) do
     begin
-      S := AnsiString(Get(0).AsStr);
+      var S := AnsiString(Get(0).AsStr);
       MakeStr(ResPtr^, SHA1DigestToString(SHA1Buf(Pointer(S)^, Length(S)*SizeOf(S[1]))));
     end;
   except
@@ -1791,14 +1783,12 @@ end;
 
 function GetSHA1OfUnicodeString(Ext: Longint; const Params: IIsppFuncParams;
   const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
-var
-  S: UnicodeString;
 begin
   if CheckParams(Params, [evStr], 1, Result) then
   try
     with IInternalFuncParams(Params) do
     begin
-      S := Get(0).AsStr;
+      var S := Get(0).AsStr;
       MakeStr(ResPtr^, SHA1DigestToString(SHA1Buf(Pointer(S)^, Length(S)*SizeOf(S[1]))));
     end;
   except
@@ -1810,6 +1800,76 @@ begin
   end;
 end;
 
+function GetSHA256OfFile(Ext: Longint; const Params: IIsppFuncParams;
+  const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
+begin
+  if CheckParams(Params, [evStr], 1, Result) then
+  try
+    with IInternalFuncParams(Params) do
+    begin
+      MakeStr(ResPtr^, THashSHA2.GetHashStringFromFile(Get(0).AsStr, SHA256));
+    end;
+  except
+    on E: Exception do
+    begin
+      FuncResult.Error(PChar(E.Message));
+      Result.Error := ISPPFUNC_FAIL
+    end;
+  end;
+end;
+
+function GetSHA256OfString(Ext: Longint; const Params: IIsppFuncParams;
+  const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
+begin
+  if CheckParams(Params, [evStr], 1, Result) then
+  try
+    with IInternalFuncParams(Params) do
+    begin
+      var S := AnsiString(Get(0).AsStr);
+      var M := TMemoryStream.Create;
+      try
+        M.Write(Pointer(S)^, Length(S)*SizeOf(S[1]));
+        M.Seek(0, soFromBeginning);
+        MakeStr(ResPtr^, THashSHA2.GetHashString(M, SHA256));
+      finally
+        M.Free;
+      end;
+    end;
+  except
+    on E: Exception do
+    begin
+      FuncResult.Error(PChar(E.Message));
+      Result.Error := ISPPFUNC_FAIL
+    end;
+  end;
+end;
+
+function GetSHA256OfUnicodeString(Ext: Longint; const Params: IIsppFuncParams;
+  const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
+begin
+  if CheckParams(Params, [evStr], 1, Result) then
+  try
+    with IInternalFuncParams(Params) do
+    begin
+      var S := Get(0).AsStr;
+      var M := TMemoryStream.Create;
+      try
+        M.Write(Pointer(S)^, Length(S)*SizeOf(S[1]));
+        M.Seek(0, soFromBeginning);
+        MakeStr(ResPtr^, THashSHA2.GetHashString(M, SHA256));
+      finally
+        M.Free;
+      end;
+    end;
+  except
+    on E: Exception do
+    begin
+      FuncResult.Error(PChar(E.Message));
+      Result.Error := ISPPFUNC_FAIL
+    end;
+  end;
+end;
+
 function TrimFunc(Ext: Longint; const Params: IIsppFuncParams;
   const FuncResult: IIsppFuncResult): TIsppFuncResult; stdcall;
 begin
@@ -2009,6 +2069,9 @@ begin
     RegisterFunction('GetSHA1OfFile', GetSHA1OfFile, -1);
     RegisterFunction('GetSHA1OfString', GetSHA1OfString, -1);
     RegisterFunction('GetSHA1OfUnicodeString', GetSHA1OfUnicodeString, -1);
+    RegisterFunction('GetSHA256OfFile', GetSHA256OfFile, -1);
+    RegisterFunction('GetSHA256OfString', GetSHA256OfString, -1);
+    RegisterFunction('GetSHA256OfUnicodeString', GetSHA256OfUnicodeString, -1);
     RegisterFunction('Trim', TrimFunc, -1);
     RegisterFunction('StringChange', StringChangeFunc, -1);
     RegisterFunction('IsWin64', IsWin64Func, -1);

+ 11 - 14
Projects/Src/Setup.InstFunc.pas

@@ -560,17 +560,16 @@ function GetMD5OfFile(const DisableFsRedir: Boolean; const Filename: String): TM
 { Gets MD5 sum of the file Filename. An exception will be raised upon
   failure. }
 var
-  F: TFile;
-  NumRead: Cardinal;
-  Context: TMD5Context;
   Buf: array[0..65535] of Byte;
 begin
+  var Context: TMD5Context;
   MD5Init(Context);
-  F := TFileRedir.Create(DisableFsRedir, Filename, fdOpenExisting, faRead, fsReadWrite);
+  var F := TFileRedir.Create(DisableFsRedir, Filename, fdOpenExisting, faRead, fsReadWrite);
   try
     while True do begin
-      NumRead := F.Read(Buf, SizeOf(Buf));
-      if NumRead = 0 then Break;
+      var NumRead := F.Read(Buf, SizeOf(Buf));
+      if NumRead = 0 then
+        Break;
       MD5Update(Context, Buf, NumRead);
     end;
   finally
@@ -583,17 +582,16 @@ function GetSHA1OfFile(const DisableFsRedir: Boolean; const Filename: String): T
 { Gets SHA-1 sum of the file Filename. An exception will be raised upon
   failure. }
 var
-  F: TFile;
-  NumRead: Cardinal;
-  Context: TSHA1Context;
   Buf: array[0..65535] of Byte;
 begin
+  var Context: TSHA1Context;
   SHA1Init(Context);
-  F := TFileRedir.Create(DisableFsRedir, Filename, fdOpenExisting, faRead, fsReadWrite);
+  var F := TFileRedir.Create(DisableFsRedir, Filename, fdOpenExisting, faRead, fsReadWrite);
   try
     while True do begin
-      NumRead := F.Read(Buf, SizeOf(Buf));
-      if NumRead = 0 then Break;
+      var NumRead := F.Read(Buf, SizeOf(Buf));
+      if NumRead = 0 then
+        Break;
       SHA1Update(Context, Buf, NumRead);
     end;
   finally
@@ -605,9 +603,8 @@ end;
 function GetSHA256OfFile(const DisableFsRedir: Boolean; const Filename: String): String;
 { Gets SHA-256 sum as a string of the file Filename. An exception will be raised upon
   failure. }
-var
-  PrevState: TPreviousFsRedirectionState;
 begin
+  var PrevState: TPreviousFsRedirectionState;
   if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then
     InternalError('GetSHA256OfFile: DisableFsRedirectionIf failed.');
   try

+ 2 - 1
whatsnew.htm

@@ -90,6 +90,7 @@ For conditions of distribution and use, see <a href="files/is/license.txt">LICEN
     <li>Documented support functions <tt>VarArrayGet</tt> and <tt>VarArraySet</tt> which were already available but not documented.</li>
   </ul>
   </li>
+  <li>ISPP change: Added support functions <tt>GetSHA256OfFile</tt>, <tt>GetSHA256OfString</tt>, and <tt>GetSHA256OfUnicodeString</tt>.</li>
   <li>Various tweaks and other documentation improvements.</li>
 </ul>
 
@@ -128,7 +129,7 @@ For conditions of distribution and use, see <a href="files/is/license.txt">LICEN
     <li>Added new <tt>SaveStringsToUTF8FileWithoutBOM</tt> support function.</li>
   </ul>
   </li>
-  <li>ISPP change: preprocessor output saved by <tt>SaveToFile</tt> is now always saved as UTF-8 without a BOM.</li>
+  <li>ISPP change: Preprocessor output saved by <tt>SaveToFile</tt> is now always saved as UTF-8 without a BOM.</li>
 </ul>
 <p><span class="head2">Support for Arm64 systems improved, and related enhancements</span></p>
 <ul>