Skip to content

Commit

Permalink
More logic errors fixed; tests and travis metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter Stanish committed Feb 18, 2016
1 parent c568639 commit 38e415c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 37 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: php
php:
- '5.3'
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- hhvm
- nightly
script: php utils/test.php
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ php-iso7064

This is a reference implementation of the various 'pure' ISO7064 algorithms in PHP. It is 100% generated code, from [algorithm metadata](https://github.com/globalcitizen/php-iso7064/blob/master/utils/algorithms.txt). Note that the 'hybrid' algorithms are not implemented.

[![Build Status](https://travis-ci.org/globalcitizen/php-iso7064.png)](https://travis-ci.org/globalcitizen/php-iso7064)
[![Latest Stable Version](https://poser.pugx.org/globalcitizen/php-iso7064/v/stable)](https://packagist.org/packages/globalcitizen/php-iso7064)
[![License](https://poser.pugx.org/globalcitizen/php-iso7064/license)](https://packagist.org/packages/globalcitizen/php-iso7064)

Algorithms implemented
----------------------

Expand All @@ -18,6 +22,7 @@ History
-------

__February 2016__
* __[Version 0.1.2](https://github.com/globalcitizen/php-iban/releases/tag/v0.1.2) released__: On the way to functionality.
* __[Version 0.1.1](https://github.com/globalcitizen/php-iban/releases/tag/v0.1.1) released__: Syntax error resolved.
* __[Version 0.1.0](https://github.com/globalcitizen/php-iban/releases/tag/v0.1.0) released__: Absolutely and completely untested! :)

Expand Down
58 changes: 29 additions & 29 deletions php-iso7064.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ function iso7064_mod11_2($input) {
$output_values = '0123456789X';
$p = 0;
for($i=0; $i<strlen($input); $i++) {
$val = $chars.indexOf($input.charAt($i));
$val = strpos($output_values,substr($input,$i,1));
if($val < 0) { return ''; } # illegal character encountered
$p = (($p + $val) * $r) % $m;
$p = (($p + $val) * $radix) % $modulus;
}
$checksum = ($m - $p + 1) % $m;
return $chars.charAt($checksum);
$checksum = ($modulus - $p + 1) % $modulus;
return substr($output_values,$checksum,1);
}

# ISO/IEC 7064, MOD 37-2
Expand All @@ -34,12 +34,12 @@ function iso7064_mod37_2($input) {
$output_values = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*';
$p = 0;
for($i=0; $i<strlen($input); $i++) {
$val = $chars.indexOf($input.charAt($i));
$val = strpos($output_values,substr($input,$i,1));
if($val < 0) { return ''; } # illegal character encountered
$p = (($p + $val) * $r) % $m;
$p = (($p + $val) * $radix) % $modulus;
}
$checksum = ($m - $p + 1) % $m;
return $chars.charAt($checksum);
$checksum = ($modulus - $p + 1) % $modulus;
return substr($output_values,$checksum,1);
}

# ISO/IEC 7064, MOD 97-10
Expand All @@ -54,15 +54,15 @@ function iso7064_mod97_10($input) {
$output_values = '0123456789';
$p = 0;
for($i=0; $i<strlen($input); $i++) {
$val = $chars.indexOf($input.charAt($i));
$val = strpos($output_values,substr($input,$i,1));
if($val < 0) { return ''; } # illegal character encountered
$p = (($p + $val) * $r) % $m;
$p = (($p + $val) * $radix) % $modulus;
}
$p = ($p*$r) % $m;
$checksum = ($m - $p + 1) % $m;
$second = $checksum % $r;
$first = ($checksum - $second) / $r;
return $chars.charAt($first) . chars.charAt($second);
$p = ($p*$radix) % $modulus;
$checksum = ($modulus - $p + 1) % $modulus;
$second = $checksum % $radix;
$first = ($checksum - $second) / $radix;
return substr($output_values,$first,2);
}

# ISO/IEC 7064, MOD 661-26
Expand All @@ -77,15 +77,15 @@ function iso7064_mod661_26($input) {
$output_values = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$p = 0;
for($i=0; $i<strlen($input); $i++) {
$val = $chars.indexOf($input.charAt($i));
$val = strpos($output_values,substr($input,$i,1));
if($val < 0) { return ''; } # illegal character encountered
$p = (($p + $val) * $r) % $m;
$p = (($p + $val) * $radix) % $modulus;
}
$p = ($p*$r) % $m;
$checksum = ($m - $p + 1) % $m;
$second = $checksum % $r;
$first = ($checksum - $second) / $r;
return $chars.charAt($first) . chars.charAt($second);
$p = ($p*$radix) % $modulus;
$checksum = ($modulus - $p + 1) % $modulus;
$second = $checksum % $radix;
$first = ($checksum - $second) / $radix;
return substr($output_values,$first,2);
}

# ISO/IEC 7064, MOD 1271-36
Expand All @@ -100,15 +100,15 @@ function iso7064_mod1271_36($input) {
$output_values = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZY';
$p = 0;
for($i=0; $i<strlen($input); $i++) {
$val = $chars.indexOf($input.charAt($i));
$val = strpos($output_values,substr($input,$i,1));
if($val < 0) { return ''; } # illegal character encountered
$p = (($p + $val) * $r) % $m;
$p = (($p + $val) * $radix) % $modulus;
}
$p = ($p*$r) % $m;
$checksum = ($m - $p + 1) % $m;
$second = $checksum % $r;
$first = ($checksum - $second) / $r;
return $chars.charAt($first) . chars.charAt($second);
$p = ($p*$radix) % $modulus;
$checksum = ($modulus - $p + 1) % $modulus;
$second = $checksum % $radix;
$first = ($checksum - $second) / $radix;
return substr($output_values,$first,2);
}

?>
16 changes: 8 additions & 8 deletions utils/generate_php-iso7064.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ function __iso7064_load_algorithms() {
print " \$output_values = '" . $__iso7064_algorithms[$algorithm]['output_values'] . "';\n"; # chars
print " \$p = 0;\n";
print " for(\$i=0; \$i<strlen(\$input); \$i++) {\n";
print " \$val = \$chars.indexOf(\$input.charAt(\$i));\n";
print " \$val = strpos(\$output_values,substr(\$input,\$i,1));\n"; # later +1?
print " if(\$val < 0) { return ''; } # illegal character encountered\n";
print " \$p = ((\$p + \$val) * \$r) % \$m;\n";
print " \$p = ((\$p + \$val) * \$radix) % \$modulus;\n";
print " }\n";
if($__iso7064_algorithms[$algorithm]['output_qty']>1) {
print "\$p = (\$p*\$r) % \$m;\n";
print "\$p = (\$p*\$radix) % \$modulus;\n";
}
print " \$checksum = (\$m - \$p + 1) % \$m;\n";
print " \$checksum = (\$modulus - \$p + 1) % \$modulus;\n";
if($__iso7064_algorithms[$algorithm]['output_qty']>1) {
print " \$second = \$checksum % \$r;\n";
print " \$first = (\$checksum - \$second) / \$r;\n";
print " return \$chars.charAt(\$first) . chars.charAt(\$second);\n";
print " \$second = \$checksum % \$radix;\n";
print " \$first = (\$checksum - \$second) / \$radix;\n";
print " return substr(\$output_values,\$first,2);\n";
}
else {
print " return \$chars.charAt(\$checksum);\n";
print " return substr(\$output_values,\$checksum,1);\n"; # later +1?
}
print "}\n\n";
}
Expand Down
48 changes: 48 additions & 0 deletions utils/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

error_reporting(E_ALL);

require_once(dirname(dirname(__FILE__))) . '/php-iso7064.php';


################# ALGORITHM ############################ INPUT ################ OUTPUT ###################
$tests = array(
'iso7064_mod11_2' => array(
'0794' => '0',
'079' => 'X'
),
'iso7064_mod1271_36' => array(
'ISO79' => '3W'
),
'iso7064_mod37_2' => array(
'G123489654321' => 'Y'
),
'iso7064_mod661_26' => array(
'BAISDLAFK' => 'BM'
),
'iso7064_mod97_10' => array(
'794' => '44',
'107571' => '07'
)
);

# Execute tests
foreach(array_keys($tests) as $algorithm) {
print "[$algorithm]\n";
foreach($tests[$algorithm] as $input=>$expected_output) {
print " - $algorithm('$input') = '$expected_output'? ... ";
$result = $algorithm($input);
if($result != $expected_output) {
print "FAILED (expected '$expected_output', received '$result').\n";
#exit(1);
}
else {
print "OK.\n";
}
}
}

print "All tests OK.\n";
exit(0);

?>

0 comments on commit 38e415c

Please sign in to comment.