Jump to content
Sign in to follow this  
warleyalex

base64

Recommended Posts

I would like to implement stringtobase64  function in SMS to perform basic authentication, something like:

 

convertStringToBase64(userName + ":" + password);

 

e.g. warley:galo = d2FybGV5YWxleDpnYWxv

 

but my function is returning...

 

convertStringToBase64("warley:galo") = d2FybGV5OmdhbG8

 

Any idea?

 

code:

 
const 

Codes64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 

function convertStringToBase64(S: string): string; 
var 
  j: Integer; 
  a: Integer; 
  x: Integer; 
  b: Integer; 
begin 
  Result := ''; 
  a := 0; 
  b := 0; 
  for j := 1 to Length(S) do 
  begin 
    x := Ord(S[j]); 
    b := b * 256 + x; 
    a := a + 8; 
    while a >= 6 do 
    begin 
      a := a - 6; 
      x := b div (1 shl a); 
      b := b mod (1 shl a); 
      Result := Result + Codes64[x + 1]; 
    end; 
  end; 

  if a > 0 then 
  begin 
    x := b shl (6 - a); 
    Result := Result + Codes64[x + 1]; 
  end; 
end; 

Share this post


Link to post
Share on other sites

Thank you for replying, that's correct.

 

I'll post a convertBase64ToString function that works fine in SMS. This can be useful using basic authentication.

 

 

function convertBase64ToString(S: string): string; 
var 
  j: Integer; 
  a: Integer; 
  x: Integer; 
  b: Integer; 
begin 
  Result := ''; 
  a := 0; 
  b := 0; 
  for j := 1 to Length(s) do 
  begin 
    x := Pos(s[j], codes64) - 1; 
    if x >= 0 then 
    begin 
      b := b * 64 + x; 
      a := a + 6; 
      if a >= 8 then 
      begin 
        a := a - 8; 
        x := b shr a; 
        b := b mod (1 shl a); 
        x := x mod 256; 
        Result := Result + chr(x); 
      end; 
    end 
    else 
      Exit; 
  end; 
end; 

Share this post


Link to post
Share on other sites

Base64-encoded strings should be always padded with '=' until the length of result string is a multiple of 4. I have modified your code to handle padding on encoding and decoding end.

 

 
const 
  Codes64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 

function Base64Encode(S: string): string; 
var 
  buffer : Integer; 
  ch     : Integer; 
  cnt    : Integer; 
  iCh    : Integer; 
  numBits: Integer; 
begin 
  Result := ''; 
  numBits := 0; 
  buffer := 0; 
  cnt := 0; 
  for iCh := 1 to Length(S) do 
  begin 
    ch := Ord(S[iCh]); 
    buffer := buffer * 256 + ch; 
    numBits := numBits + 8; 
    while numBits >= 6 do 
    begin 
      numBits := numBits - 6; 
      ch := buffer div (1 shl numBits); 
      buffer := buffer mod (1 shl numBits); 
      Result := Result + Codes64[ch + 1]; 
      Inc(cnt); 
    end; 
  end; 

  if numBits > 0 then 
  begin 
    ch := buffer shl (6 - numBits); 
    Result := Result + Codes64[ch + 1]; 
    Inc(cnt); 
  end; 
  while (cnt mod 4)  0 do begin 
    Result := Result + '='; 
    Inc(cnt); 
  end; 
end; 

function Base64Decode(S: string): string; 
var 
  bits  : Integer; 
  buffer: Integer; 
  ch    : Integer; 
  iCh   : Integer; 
begin 
  Result := ''; 
  bits := 0; 
  buffer := 0; 
  for iCh := 1 to Length(S) do 
  begin 
    ch := Pos(S[iCh], Codes64) - 1; 
    if (ch        Exit 
    else if ch >= 0 then begin 
      buffer := buffer * 64 + ch; 
      bits := bits + 6; 
      if bits >= 8 then begin 
        bits := bits - 8; 
        ch := buffer shr bits; 
        buffer := buffer mod (1 shl bits); 
        ch := ch mod 256; 
        Result := Result + chr(ch); 
      end; 
    end; 
  end; 
end; 

Share this post


Link to post
Share on other sites

Why just not use

function w3_base64encode(aValue: Variant): String; 
begin 
 if VarIsValidRef(aValue) then 
 Result := BrowserAPI.Window.btoa(aValue); 
end; 

function w3_base64decode(aValue: String): Variant; 
begin 
 if aValue<>'' then 
 Result := BrowserAPI.Window.atob(aValue) 
 else Result := Undefined; 
end;

as defined in W3System.pas?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×