Recent

Author Topic: Calling the same sha1 functions, on linux 32bits different than 64bit (SOLVED)  (Read 4225 times)

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
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][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, sha1, base64;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     Edit2: TEdit;
  20.     procedure Button1Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35. function GenerateKey(const StrToHash: string): string;
  36. var
  37.     Outstream: TStringStream;
  38.     Encoder: TBase64EncodingStream;
  39.     SHA1Digest: TSHA1Digest;
  40.   begin
  41.     Outstream := TStringStream.Create('');
  42.     try
  43.       Encoder := TBase64EncodingStream.Create(outstream);
  44.       try
  45.         SHA1Digest := SHA1String(StrToHash+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11');
  46.         Encoder.Flush;
  47.         Encoder.Write(SHA1Digest, Length(SHA1Digest));
  48.       finally
  49.         Encoder.Free;
  50.       end;
  51.       Result:=Outstream.DataString;
  52.     finally
  53.       Outstream.Free;
  54.     end;
  55.  
  56.   end;
  57.  
  58. procedure TForm1.Button1Click(Sender: TObject);
  59. begin
  60.    edit2.Text:=GenerateKey('test');
  61. end;
  62.  
  63. end.  
  64.  

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
« Last Edit: April 22, 2020, 03:57:10 am by esvignolo »

john horst

  • Jr. Member
  • **
  • Posts: 68
    • JHorst
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
« Last Edit: April 20, 2020, 08:18:16 am by john horst »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
I also can not replicate it on linux-armhf (which is 32 bit) Raspbian.
Specialize a type, not a var.

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
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

Hi, thanks for testing. I use the trunk FPC SVN Revision 62571M and now update today, and the bug still there.

« Last Edit: April 20, 2020, 11:46:56 pm by esvignolo »

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
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.


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?

-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
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?



« Last Edit: April 21, 2020, 08:56:36 pm by esvignolo »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
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
I 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.

 

TinyPortal © 2005-2018