//////// Binary
////////
////////
////////

unit
  FMCUtilsWrapper;

  
interface

function Base64DecodeString(const AData: AnsiString): AnsiString;
function Base64EncodeString(const AData: AnsiString): AnsiString;

function SHA256HashString(const AData: AnsiString): AnsiString;
function SHA512HashString(const AData: AnsiString): AnsiString;

function AES256DecodeString(const AData, AKey: AnsiString): AnsiString;
function AES256EncodeString(const AData, AKey: AnsiString): AnsiString;

function AES128DecodeString(const AData, AKey: AnsiString): AnsiString;
function AES128EncodeString(const AData, AKey: AnsiString): AnsiString;

implementation

const
  CFMCUtilsLibraryFileName = 'FMCUtils.dll';

function InternalBase64DecodeString(ADataBuffer, AResultBuffer: PAnsiChar; AResultBufferSize: Integer): Boolean; stdcall; external CFMCUtilsLibraryFileName Name 'Base64DecodeString';
function InternalBase64EncodeString(ADataBuffer, AResultBuffer: PAnsiChar; AResultBufferSize: Integer): Boolean; stdcall; external CFMCUtilsLibraryFileName Name 'Base64EncodeString';

function InternalSHA256HashString(ADataBuffer, AResultBuffer: PAnsiChar; AResultBufferSize: Integer): Boolean; stdcall; external CFMCUtilsLibraryFileName Name 'SHA256HashString';
function InternalSHA512HashString(ADataBuffer, AResultBuffer: PAnsiChar; AResultBufferSize: Integer): Boolean; stdcall; external CFMCUtilsLibraryFileName Name 'SHA512HashString';

function InternalAES256DecodeString(ADataBuffer, AKeyBuffer, AResultBuffer: PAnsiChar; AResultBufferSize: Integer): Boolean; stdcall; external CFMCUtilsLibraryFileName Name 'AES256DecodeString';
function InternalAES256EncodeString(ADataBuffer, AKeyBuffer, AResultBuffer: PAnsiChar; AResultBufferSize: Integer): Boolean; stdcall; external CFMCUtilsLibraryFileName Name 'AES256EncodeString';

function InternalAES128DecodeString(ADataBuffer, AKeyBuffer, AResultBuffer: PAnsiChar; AResultBufferSize: Integer): Boolean; stdcall; external CFMCUtilsLibraryFileName Name 'AES128DecodeString';
function InternalAES128EncodeString(ADataBuffer, AKeyBuffer, AResultBuffer: PAnsiChar; AResultBufferSize: Integer): Boolean; stdcall; external CFMCUtilsLibraryFileName Name 'AES128EncodeString';

////////////////

const
  CDefaultBufferSize = 4096;

function Base64DecodeString(const AData: AnsiString): AnsiString;
var
  LResult: AnsiString;
begin
  Result := '';

  if AData <> '' then
  begin
    SetLength(LResult, CDefaultBufferSize);

    if InternalBase64DecodeString(PAnsiChar(AData), PAnsiChar(LResult), CDefaultBufferSize) then
    begin
      Result := PAnsiChar(LResult);
    end;
  end;
end;

function Base64EncodeString(const AData: AnsiString): AnsiString;
var
  LResult: AnsiString;
begin
  Result := '';

  if AData <> '' then
  begin
    SetLength(LResult, CDefaultBufferSize);

    if InternalBase64EncodeString(PAnsiChar(AData), PAnsiChar(LResult), CDefaultBufferSize) then
    begin
      Result := PAnsiChar(LResult);
    end;
  end;
end;

function SHA256HashString(const AData: AnsiString): AnsiString;
var
  LResult: AnsiString;
begin
  Result := '';

  if AData <> '' then
  begin
    SetLength(LResult, CDefaultBufferSize);

    if InternalSHA256HashString(PAnsiChar(AData), PAnsiChar(LResult), CDefaultBufferSize) then
    begin
      Result := PAnsiChar(LResult);
    end;
  end;
end;

function SHA512HashString(const AData: AnsiString): AnsiString;
var
  LResult: AnsiString;
begin
  Result := '';

  if AData <> '' then
  begin
    SetLength(LResult, CDefaultBufferSize);

    if InternalSHA512HashString(PAnsiChar(AData), PAnsiChar(LResult), CDefaultBufferSize) then
    begin
      Result := PAnsiChar(LResult);
    end;
  end;
end;

function AES256DecodeString(const AData, AKey: AnsiString): AnsiString;
var
  LResult: AnsiString;
begin
  Result := '';

  if (AData <> '') and (AKey <> '') then
  begin
    SetLength(LResult, CDefaultBufferSize);

    if InternalAES256DecodeString(PAnsiChar(AData), PAnsiChar(AKey), PAnsiChar(LResult), CDefaultBufferSize) then
    begin
      Result := PAnsiChar(LResult);
    end;
  end;
end;

function AES256EncodeString(const AData, AKey: AnsiString): AnsiString;
var
  LResult: AnsiString;
begin
  Result := '';

  if (AData <> '') and (AKey <> '') then
  begin
    SetLength(LResult, CDefaultBufferSize);

    if InternalAES256EncodeString(PAnsiChar(AData), PAnsiChar(AKey), PAnsiChar(LResult), CDefaultBufferSize) then
    begin
      Result := PAnsiChar(LResult);
    end;
  end;
end;

function AES128DecodeString(const AData, AKey: AnsiString): AnsiString;
var
  LResult: AnsiString;
begin
  Result := '';

  if (AData <> '') and (AKey <> '') then
  begin
    SetLength(LResult, CDefaultBufferSize);

    if InternalAES128DecodeString(PAnsiChar(AData), PAnsiChar(AKey), PAnsiChar(LResult), CDefaultBufferSize) then
    begin
      Result := PAnsiChar(LResult);
    end;
  end;
end;

function AES128EncodeString(const AData, AKey: AnsiString): AnsiString;
var
  LResult: AnsiString;
begin
  Result := '';

  if (AData <> '') and (AKey <> '') then
  begin
    SetLength(LResult, CDefaultBufferSize);

    if InternalAES128EncodeString(PAnsiChar(AData), PAnsiChar(AKey), PAnsiChar(LResult), CDefaultBufferSize) then
    begin
      Result := PAnsiChar(LResult);
    end;
  end;
end;
end.

