Calling the same sha1 functions, on linux 32bits different than 64bit (SOLVED) lazarus / 66-Unix ---------------------------------------------------------------------- esvignolo · 2020-04-19 22:56 Hello, I wanted to consult you. If you know why this can happen. The same functions in 64bits return different result in Linux 32bits, than in 64bits. Also try on windows32 and macos64, they return the same value as linux64. But linux32 returns different values. Here is an example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, sha1, base64; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Edit1: TEdit;    Label1: TLabel;    Label2: TLabel;    Edit2: TEdit;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 }function GenerateKey(const StrToHash: string): string;var    Outstream: TStringStream;    Encoder: TBase64EncodingStream;    SHA1Digest: TSHA1Digest;  begin    Outstream := TStringStream.Create('');    try      Encoder := TBase64EncodingStream.Create(outstream);      try        SHA1Digest := SHA1String(StrToHash+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11');        Encoder.Flush;        Encoder.Write(SHA1Digest, Length(SHA1Digest));    finally        Encoder.Free;      end;      Result:=Outstream.DataString;    finally      Outstream.Free;    end;   end; procedure TForm1.Button1Click(Sender: TObject);begin   edit2.Text:=GenerateKey('test');end; end. The result must be: tNpbgC8ZQDOcSkHAWopKzQjJ1hI= But in linux32 is not (the value return is: AoVU8zR2n+1MVbedLj2kKV4v5aQ=). UPDATE: The result in Linux32, changes in every run  SOLUTION: isn't the best but, i copy sha1.pp into application's directory and add: // Normally, if an optimized version is available for OS/CPU, that will be used // Define to use existing unoptimized implementation { the assembler implementation does not work on darwin } {$ifdef darwin} {$DEFINE SHA1PASCAL} {$endif darwin} {$IFDEF LINUX} {$IFDEF CPU32} {$DEFINE SHA1PASCAL} {$ENDIF} {$ENDIF} Now works fine ---------------------------------------------------------------------- john horst · 2020-04-20 06:13 Must be a bug in whatever version of the compiler you are using. Linux 32 (Ubuntu 16.04) / Free Pascal Compiler version 3.3.1-r20:41871M [2019/04/15] for i386 and Free Pascal Compiler version 3.0.4 [2017/10/02] for i386 Linux 64 (UBUNTU 19.04) / Free Pascal Compiler version 3.2.0-beta-r44874 [2020/04/19] for x86_64 I get the expected result of tNpbgC8ZQDOcSkHAWopKzQjJ1hI= everytime ---------------------------------------------------------------------- Thaddy · 2020-04-20 07:44 I also can not replicate it on linux-armhf (which is 32 bit) Raspbian. ---------------------------------------------------------------------- esvignolo · 2020-04-20 21:35 Quote from: john horst on April 20, 2020, 08:13:58 amMust be a bug in whatever version of the compiler you are using. Linux 32 (Ubuntu 16.04) / Free Pascal Compiler version 3.3.1-r20:41871M [2019/04/15] for i386 and Free Pascal Compiler version 3.0.4 [2017/10/02] for i386 Linux 64 (UBUNTU 19.04) / Free Pascal Compiler version 3.2.0-beta-r44874 [2020/04/19] for x86_64 I get the expected result of tNpbgC8ZQDOcSkHAWopKzQjJ1hI= everytime Hi, thanks for testing. I use the trunk FPC SVN Revision 62571M and now update today, and the bug still there. ---------------------------------------------------------------------- ASBzone · 2020-04-21 02:07 Quote from: esvignolo on April 20, 2020, 12:56:32 amHello, I wanted to consult you. If you know why this can happen. The same functions in 64bits return different result in Linux 32bits, than in 64bits. Also try on windows32 and macos64, they return the same value as linux64. But linux32 returns different values. The result must be: tNpbgC8ZQDOcSkHAWopKzQjJ1hI= But in linux32 is not (the value return is: AoVU8zR2n+1MVbedLj2kKV4v5aQ=). UPDATE: The result in Linux32, changes in every run  Thanks!!! What version of Linux, FPC and Lazarus? Also, are using the 32-bit version of FPC or the 64-bit version and cross-compiling? ---------------------------------------------------------------------- esvignolo · 2020-04-21 02:11 Hi! Linux mint buster 64 bits. FPC Cross compiling for 32bits (with fpcupdeluxe) Lazarus 2.1.0 r62571M FPC 3.3.1 x86_64-linux-gtk2 Here is the problem: SHA1Digest := SHA1String(StrToHash+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'); Running the test in linux 32: esteban@Linux-mint:~/development/fpcsrc/packages/hash/examples$ ./sha1test Performing basic SHA-1 test... SHA-1 tests failed: 7 I tried {$DEFINE SHA1PASCAL}  with no luck Any ideas? ---------------------------------------------------------------------- Leledumbo · 2020-04-21 19:28 I can reproduce the issue: Code: [Select]$ cat sha1test.pas uses   sha1; begin   WriteLn(SHA1Print(SHA1String('test258EAFA5-E914-47DA-95CA- C5AB0DC85B11'))); end. $ fpc -l -Pi386 sha1test.pas && ./sha1test Hint: End of reading config file /etc/fpc.cfg Free Pascal Compiler version 3.3.1 [2020/04/22] for i386 Copyright (c) 1993-2020 by Florian Klaempfl and others Target OS: Linux for i386 Compiling sha1test.pas Linking sha1test 6 lines compiled, 0.1 sec 1 hint(s) issued de654445af78fa61244e4a1a1c56c5111be90378 $ fpc -l -Px86_64 sha1test.pas && ./sha1test Hint: End of reading config file /etc/fpc.cfg Free Pascal Compiler version 3.3.1 [2020/04/22] for x86_64 Copyright (c) 1993-2020 by Florian Klaempfl and others Target OS: Linux for x86-64 Compiling sha1test.pas Linking sha1test 6 lines compiled, 0.1 sec 1 hint(s) issued b4da5b802f1940339c4a41c05a8a4acd08c9d612 Quote from: esvignolo on April 21, 2020, 04:11:55 amI tried {$DEFINE SHA1PASCAL}  with no luck Useless if you define it for your project. You must recompile sha1 unit with this define instead, but I've tried this and the result was identical.