Einzelnen Beitrag anzeigen

Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.021 Beiträge
 
Delphi 12 Athens
 
#10

AW: Base32 Implementation

  Alt 9. Feb 2017, 10:43
Ja, das liegt wohl am Indy-Decoding. So funktioniert es:
Delphi-Quellcode:
uses
  System.SysUtils,
  System.NetEncoding;

const
  base32chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';

function Base32Encode(source: TBytes): string;
var
  i: integer;
  L: Integer;
  nr: int64;
  offset: Integer;
begin
  result := '';
  offset := 0;
  L := length(source);
  while L > 0 do
  begin
    nr := 0;
    for i := 0 to 4 do
    begin
      nr := (nr shl 8);
      if i < L then begin
        nr := nr + source[offset + i];
      end;
    end;
    for i := 7 downto 0 do
      if ((L<2) and (i<6)) or
         ((L<3) and (i<4)) or
         ((L<4) and (i<3)) or
         ((L<5) and (i<1)) then
      result := result + '='
    else
      result := result + base32chars[((nr shr (i*5)) and $1F)+1];
    Inc(Offset, 5);
    Dec(L, Offset);
  end;
end;

procedure Main();
begin
  Assert(Base32Encode(TNetEncoding.Base64.DecodeStringToBytes('DngfhpghKu8='))='BZ4B7BUYEEVO6===');
end;
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat