Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Build and run a StreamSec application on Linux with Free Pascal

Build a native x86-64 Linux command-line program that prints the SHA-256 digest of a file. Free Pascal compiles the required StreamSec units from source.

Prepare the native toolchain

Use Free Pascal 3.2.2 for x86-64 Linux with its matching RTL and FCL units, the system linker and the libc development files. Run fpc -iV to check the compiler version. fpc -iTP should print x86_64 and fpc -iTO should print linux. These instructions cover the hash units used below.

Copy the full StreamSec source directory to Linux, preserving the case of the file names. In your shell set STREAMSEC_SRC to the absolute path of the directory containing stSHA256.pas and ver.inc. For example, STREAMSEC_SRC="/opt/streamsec/Source". Work in a separate directory for your program.

Create the program

Save the following unit as TaskFpcLinux.pas. The digest covers the bytes from the stream's current position to its end.

unit TaskFpcLinux;

interface

uses
  Classes;

function FpcSha256Stream(aInput: TStream): string;

implementation

uses
  SysUtils, stSHA256, stSecUtils;

function FpcSha256Stream(aInput: TStream): string;
var
  lHash: tSHA256;
  lDigest: TBytes;
  lBuffer: array[0..8191] of Byte;
  lCount: Integer;
begin
  lHash := tSHA256.CreateUnkeyed(False);
  try
    repeat
      lCount := aInput.Read(lBuffer, SizeOf(lBuffer));
      if lCount < 0 then
        raise EReadError.Create('Error reading the input stream');
      if lCount > 0 then
        lHash.HashData(lBuffer[0], lCount);
    until lCount = 0;
    lHash.DoneBytes(lDigest);
    Result := LowerCase(stSecUtils.BinToHex(lDigest[0], Length(lDigest)));
  finally
    lHash.Free;
  end;
end;

end.

In the same directory, save the program as FpcHashDemo.dpr. Free Pascal on Linux initializes its thread and wide-string managers before loading the library units.

program FpcHashDemo;

{$IFDEF MSWINDOWS}
{$APPTYPE CONSOLE}
{$ENDIF}

uses
  {$IFDEF FPC}{$IFDEF UNIX}cthreads, cwstring,{$ENDIF}{$ENDIF}
  Classes, SysUtils, TaskFpcLinux;

var
  lFile: TFileStream;
begin
  if ParamCount <> 1 then begin
    Writeln(ErrOutput, 'Usage: FpcHashDemo FILE');
    Halt(2);
  end;
  try
    lFile := TFileStream.Create(ParamStr(1), fmOpenRead or fmShareDenyWrite);
    try
      Writeln(FpcSha256Stream(lFile));
    finally
      lFile.Free;
    end;
  except
    on E: Exception do begin
      Writeln(ErrOutput, E.Message);
      Halt(1);
    end;
  end;
end.

Compile and run

  1. Create separate application and compiled-unit directories with mkdir -p build/bin build/units.
  2. Compile with fpc -B -Mdelphiunicode -Fu"$STREAMSEC_SRC" -Fi"$STREAMSEC_SRC" -FUbuild/units -FEbuild/bin -oFpcHashDemo FpcHashDemo.dpr.
  3. Create a three-byte input file with printf '%s' abc > input.bin, then run ./build/bin/FpcHashDemo input.bin.

The compiler uses -Fu to find Pascal units and -Fi to find includes. -FU selects the output directory for compiled units and -FE selects the directory for the executable. The mode switch applies Delphi Unicode string semantics throughout the build. Keep separate output directories when changing compiler version or target.

For the three bytes abc, the program prints ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad. To deploy it on a compatible x86-64 Linux host, copy build/bin/FpcHashDemo with its execute permission. Run ldd build/bin/FpcHashDemo on the build host to identify the shared libraries the target needs. Run the same three-byte check there.

Reference

stSHA256 · stSecUtils

Related tasks