Luhn algorithm/fr

From Free Pascal wiki
Revision as of 07:00, 19 February 2020 by Trev (talk | contribs) (Fixed syntax highlighting)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

English (en) français (fr)

Cet algorithme a été créé par Hans Peter Luhn. Il est spécifié dans la norme ISO/IEC 7812-1 et est utilisé par certaines cartes de crédit par exemple.

function LuhnAlgorithmCheckDigit(const s:string):integer;
var
  l, i, sum, deo : integer;
begin
  l := length( s );
  sum := 0;
  for i := l downto 1 do
    begin
      if odd(i) then sum := sum + StrToInt(s[i])
      else
        begin
          deo :=  StrToInt(s[i]) * 2 ;
          if deo  > 9 then deo := deo mod 10 + 1;
          sum := sum + deo;
        end;
    end;
  result := 10 - sum mod 10;
end;

function isLuhnValid( const s : string ): boolean;
var
  i, l :integer;
begin
  l := length( s );
  i := LuhnAlgorithmCheckDigit( copy( s, 1, l-1) );
  result :=  i  = StrToInt( s[ l ] );
end;