Permalink
Browse files

Add PHP implementation by Sandro Noel

  • Loading branch information...
Gleb Dolgich
Gleb Dolgich committed Oct 30, 2011
1 parent 4f4097c commit d5fa30dd9fab56e0c605234c51d1ea576bf981b1
View
@@ -0,0 +1,120 @@
+<?php
+function base32_encode($input) {
+ // Get a binary representation of $input
+ $binary = unpack('C*', $input);
+ $binary = vsprintf(str_repeat('%08b', count($binary)), $binary);
+
+ $binaryLength = strlen($binary);
+ $base32_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
+ $currentPosition = 0;
+ $output = '';
+
+ while($currentPosition < $binaryLength) {
+ $bits = substr($binary, $currentPosition, 5);
+
+ if(strlen($bits) < 5)
+ $bits = str_pad($bits, 5, "0");
+
+ // Convert the 5 bits into a decimal number
+ // and append the matching character to $output
+ $output .= $base32_characters[bindec($bits)];
+ $currentPosition += 5;
+ }
+ // Pad the output length to a multiple of 8 with '=' characters
+ $desiredOutputLength = strlen($output);
+ if($desiredOutputLength % 8 != 0) {
+ $desiredOutputLength += (8 - ($desiredOutputLength % 8));
+ $output = str_pad($output, $desiredOutputLength, "=");
+ }
+ return $output;
+}
+
+
+
+
+function Base32_decode($inStr) {
+
+ $inString = strtolower(rtrim(str_replace("=", "", $inStr)));
+ /* declaration */
+ $inputCheck = null;
+ $deCompBits = null;
+
+ $BASE32_TABLE = array(
+ 0x61 => '00000',
+ 0x62 => '00001',
+ 0x63 => '00010',
+ 0x64 => '00011',
+ 0x65 => '00100',
+ 0x66 => '00101',
+ 0x67 => '00110',
+ 0x68 => '00111',
+ 0x69 => '01000',
+ 0x6a => '01001',
+ 0x6b => '01010',
+ 0x6c => '01011',
+ 0x6d => '01100',
+ 0x6e => '01101',
+ 0x6f => '01110',
+ 0x70 => '01111',
+ 0x71 => '10000',
+ 0x72 => '10001',
+ 0x73 => '10010',
+ 0x74 => '10011',
+ 0x75 => '10100',
+ 0x76 => '10101',
+ 0x77 => '10110',
+ 0x78 => '10111',
+ 0x79 => '11000',
+ 0x7a => '11001',
+ 0x32 => '11010',
+ 0x33 => '11011',
+ 0x34 => '11100',
+ 0x35 => '11101',
+ 0x36 => '11110',
+ 0x37 => '11111',
+ );
+
+ /* Step 1 */
+ $inputCheck = strlen($inString) % 8;
+ if(($inputCheck == 1)||($inputCheck == 3)||($inputCheck == 6)) {
+ trigger_error('input to Base32Decode was a bad mod length: '.$inputCheck);
+ return false;
+ //return $this->raiseError('input to Base32Decode was a bad mod length: '.$inputCheck, null,
+ // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
+ }
+
+ /* $deCompBits is a string that represents the bits as 0 and 1.*/
+ for ($i = 0; $i < strlen($inString); $i++) {
+ $inChar = ord(substr($inString,$i,1));
+ if(isset($BASE32_TABLE[$inChar])) {
+ $deCompBits .= $BASE32_TABLE[$inChar];
+ } else {
+ trigger_error('input to Base32Decode had a bad character: '.$inChar.":".substr($inString,$i,1));
+ return false;
+ //return $this->raiseError('input to Base32Decode had a bad character: '.$inChar, null,
+ // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
+ }
+ }
+
+ /* Step 5 */
+ $padding = strlen($deCompBits) % 8;
+ $paddingContent = substr($deCompBits, (strlen($deCompBits) - $padding));
+ if(substr_count($paddingContent, '1')>0) {
+ trigger_error('found non-zero padding in Base32Decode');
+ return false;
+ //return $this->raiseError('found non-zero padding in Base32Decode', null,
+ // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
+ }
+
+ /* Break the decompressed string into octets for returning */
+ $deArr = array();
+ for($i = 0; $i < (int)(strlen($deCompBits) / 8); $i++) {
+ $deArr[$i] = chr(bindec(substr($deCompBits, $i*8, 8)));
+ }
+
+ $outString = join('',$deArr);
+
+ return $outString;
+}
+
+?>
View
@@ -0,0 +1,8 @@
+-----BEGIN DSA PRIVATE KEY-----
+MIH4AgEAAkEA5P9B2i+I6OAhGq9vH7xqdVeOYYHlS33GcIjLQlqurwNayQHa0fXM
+LLR0MEZljjiXq3cfJDrhHHDZdjsWm9KE5wIVAPWa2+7ZOUsA9S1FRIj+mzIPzwPD
+AkEAgc0H1l7vhB6euIEh0AisnfHsPtKqAhmqgMkf5qh1+QV8AH/tgt2OmGxfCU4j
+J4I6SSfSPj8Y2sZ8L18hV1rAMAJAIaxbtIbsR4bo5FVOqzTPu0YPJu8vYef/4aqc
+5OsmLa9wa9LbZdN5IjsSzXaEgUG+rOxRH/KRhcL8dSO28ZrTsgIUQNQt9O/9KxdT
+iEmx/5yYySLhYHQ=
+-----END DSA PRIVATE KEY-----
View
@@ -0,0 +1,8 @@
+-----BEGIN PUBLIC KEY-----
+MIHxMIGpBgcqhkjOOAQBMIGdAkEA5P9B2i+I6OAhGq9vH7xqdVeOYYHlS33GcIjL
+QlqurwNayQHa0fXMLLR0MEZljjiXq3cfJDrhHHDZdjsWm9KE5wIVAPWa2+7ZOUsA
+9S1FRIj+mzIPzwPDAkEAgc0H1l7vhB6euIEh0AisnfHsPtKqAhmqgMkf5qh1+QV8
+AH/tgt2OmGxfCU4jJ4I6SSfSPj8Y2sZ8L18hV1rAMANDAAJAIaxbtIbsR4bo5FVO
+qzTPu0YPJu8vYef/4aqc5OsmLa9wa9LbZdN5IjsSzXaEgUG+rOxRH/KRhcL8dSO2
+8ZrTsg==
+-----END PUBLIC KEY-----
@@ -0,0 +1,22 @@
+<form method="POST" action="<?php $this ?>">
+ <dl>
+ <dt><label for="pCode">Product Code</label></dt>
+ <dt><input type="text" name="pCode" value="<?php $_POST["pCode"]?>" id="pCode" /></dt>
+ <dt><label for="name">Name</label></dt>
+ <dt><input type="text" name="name" value="<?php $_POST["name"]?>" id="name" /></dt>
+ <dt><label for="email">e-Mail</label></dt>
+ <dt><input type="text" name="email" value="<?php $_POST["email"]?>" id="email" /></dt>
+ <dt><input type="submit"/></dt>
+ </dl>
+</form>
+
+<?php
+ if ($_POST["pCode"] && $_POST["name"] && $_POST["email"]){
+
+ include_once('license_generator.php');
+ $lic = new License_Generator;
+ $code = $lic->make_lincese($_POST["pCode"], $_POST["name"], $_POST["email"]);
+ echo 'your License:<br><strong>' . $code ."</strong>";
+ }
+?>
+
@@ -0,0 +1,84 @@
+<?php
+include('base32.php');
+class License_Generator
+{
+ var $private_key;
+ var $public_key;
+
+ #-#############################################
+ # desc: constructor
+ function License_Generator(){
+
+
+ ## NOTE ###############################################
+ # Point the key loading functions to your private and public keys on your server.
+
+ //read the Private Key from disk
+ $this->private_key = file_get_contents('./dsa_priv.pem', FILE_USE_INCLUDE_PATH);
+ //read the public Key from disk
+ $this->public_key = file_get_contents('./dsa_pub.pem', FILE_USE_INCLUDE_PATH);
+ }#-#constructor()
+
+ #-#############################################
+ # desc: Create a license
+ public function make_lincese($product_code, $name, $email)
+ {
+ ## NOTE ###############################################
+ # If you change the parameters the function acepts do not
+ # forget to change the lower string concatenation
+ # to include all fields in the license generation
+
+ $stringData = $product_code.",".$name.",".$email;
+
+ #################################################
+ $binary_signature ="";
+ openssl_sign($stringData, $binary_signature, $this->private_key, OPENSSL_ALGO_DSS1);
+ // base 32 encode the signature
+ $encoded = base32_encode($binary_signature);
+ // replace O with 8 and I with 9
+ $replacement = str_replace("O", "8", str_replace("I", "9", $encoded));
+ //remove padding if any.
+ $padding = trim(str_replace("=", "", $replacement));
+ $dashed = rtrim(chunk_split($padding, 5,"-"));
+ $theKey = substr($dashed, 0 , strlen($dashed) -1);
+
+ return $theKey;
+ }
+
+ #-#############################################
+ # desc: Verify License
+ public function verify_license($product_code, $name, $email, $license)
+ {
+ ## NOTE ###############################################
+ # If you change the parameters the function acepts do not
+ # forget to change the lower string concatenation
+ # to include all fields in the license generation
+
+ $stringData = $product_code.",".$name.",".$email;
+
+ #################################################
+ // replace O with 8 and I with 9
+ $replacement = str_replace("8", "O", str_replace("9", "I", $license));
+ //remove Dashes.
+ $undashed = trim(str_replace("-", "", $replacement));
+ // Pad the output length to a multiple of 8 with '=' characters
+ $desiredLength = strlen($undashed);
+ if($desiredLength % 8 != 0) {
+ $desiredLength += (8 - ($desiredLength % 8));
+ $undashed = str_pad($undashed, $desiredLength, "=");
+ }
+ // decode Key
+ $decodedHash = base32_decode($undashed);
+
+ $ok = openssl_verify($stringData, $decodedHash, $this->public_key, OPENSSL_ALGO_DSS1);
+ if ($ok == 1) {
+ return TRUE;
+ } elseif ($ok == 0) {
+ return FALSE;
+ } else {
+ return FALSE;
+ }
+ }
+
+} # Class License
+?>
@@ -0,0 +1,25 @@
+<form method="post" action="<?php $this ?>">
+ <dl>
+ <dt><label for="pCode">Product Code</label></dt>
+ <dt><input type="text" name="pCode" value="<?php $_POST["pCode"]?>" id="pCode" /></dt>
+ <dt><label for="name">Name</label></dt>
+ <dt><input type="text" name="name" value="<?php $_POST["name"]?>" id="name" /></dt>
+ <dt><label for="email">e-Mail</label></dt>
+ <dt><input type="text" name="email" value="<?php $_POST["email"]?>" id="email" /></dt>
+ <dt><label for="key">Enter License Key</label></dt>
+ <dt><input type="text" name="key" value="" id="key" /></dd>
+ <dt><input type="submit"/></dd>
+ </dl>
+</form>
+
+<?php
+ include_once('license_generator.php');
+ if ($_POST["key"] && $_POST["pCode"] && $_POST["name"] && $_POST["email"]){
+ $lic = new License_Generator;
+ $result = $lic->verify_license($_POST["pCode"], $_POST["name"], $_POST["email"],$_POST["key"]);
+ if ($result)
+ echo "Valid";
+ else
+ echo "Invalid";
+ }
+?>
View
@@ -0,0 +1,120 @@
+<?php
+function base32_encode($input) {
+ // Get a binary representation of $input
+ $binary = unpack('C*', $input);
+ $binary = vsprintf(str_repeat('%08b', count($binary)), $binary);
+
+ $binaryLength = strlen($binary);
+ $base32_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
+ $currentPosition = 0;
+ $output = '';
+
+ while($currentPosition < $binaryLength) {
+ $bits = substr($binary, $currentPosition, 5);
+
+ if(strlen($bits) < 5)
+ $bits = str_pad($bits, 5, "0");
+
+ // Convert the 5 bits into a decimal number
+ // and append the matching character to $output
+ $output .= $base32_characters[bindec($bits)];
+ $currentPosition += 5;
+ }
+ // Pad the output length to a multiple of 8 with '=' characters
+ $desiredOutputLength = strlen($output);
+ if($desiredOutputLength % 8 != 0) {
+ $desiredOutputLength += (8 - ($desiredOutputLength % 8));
+ $output = str_pad($output, $desiredOutputLength, "=");
+ }
+ return $output;
+}
+
+
+
+
+function Base32_decode($inStr) {
+
+ $inString = strtolower(rtrim(str_replace("=", "", $inStr)));
+ /* declaration */
+ $inputCheck = null;
+ $deCompBits = null;
+
+ $BASE32_TABLE = array(
+ 0x61 => '00000',
+ 0x62 => '00001',
+ 0x63 => '00010',
+ 0x64 => '00011',
+ 0x65 => '00100',
+ 0x66 => '00101',
+ 0x67 => '00110',
+ 0x68 => '00111',
+ 0x69 => '01000',
+ 0x6a => '01001',
+ 0x6b => '01010',
+ 0x6c => '01011',
+ 0x6d => '01100',
+ 0x6e => '01101',
+ 0x6f => '01110',
+ 0x70 => '01111',
+ 0x71 => '10000',
+ 0x72 => '10001',
+ 0x73 => '10010',
+ 0x74 => '10011',
+ 0x75 => '10100',
+ 0x76 => '10101',
+ 0x77 => '10110',
+ 0x78 => '10111',
+ 0x79 => '11000',
+ 0x7a => '11001',
+ 0x32 => '11010',
+ 0x33 => '11011',
+ 0x34 => '11100',
+ 0x35 => '11101',
+ 0x36 => '11110',
+ 0x37 => '11111',
+ );
+
+ /* Step 1 */
+ $inputCheck = strlen($inString) % 8;
+ if(($inputCheck == 1)||($inputCheck == 3)||($inputCheck == 6)) {
+ trigger_error('input to Base32Decode was a bad mod length: '.$inputCheck);
+ return false;
+ //return $this->raiseError('input to Base32Decode was a bad mod length: '.$inputCheck, null,
+ // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
+ }
+
+ /* $deCompBits is a string that represents the bits as 0 and 1.*/
+ for ($i = 0; $i < strlen($inString); $i++) {
+ $inChar = ord(substr($inString,$i,1));
+ if(isset($BASE32_TABLE[$inChar])) {
+ $deCompBits .= $BASE32_TABLE[$inChar];
+ } else {
+ trigger_error('input to Base32Decode had a bad character: '.$inChar.":".substr($inString,$i,1));
+ return false;
+ //return $this->raiseError('input to Base32Decode had a bad character: '.$inChar, null,
+ // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
+ }
+ }
+
+ /* Step 5 */
+ $padding = strlen($deCompBits) % 8;
+ $paddingContent = substr($deCompBits, (strlen($deCompBits) - $padding));
+ if(substr_count($paddingContent, '1')>0) {
+ trigger_error('found non-zero padding in Base32Decode');
+ return false;
+ //return $this->raiseError('found non-zero padding in Base32Decode', null,
+ // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
+ }
+
+ /* Break the decompressed string into octets for returning */
+ $deArr = array();
+ for($i = 0; $i < (int)(strlen($deCompBits) / 8); $i++) {
+ $deArr[$i] = chr(bindec(substr($deCompBits, $i*8, 8)));
+ }
+
+ $outString = join('',$deArr);
+
+ return $outString;
+}
+
+?>
Oops, something went wrong.

0 comments on commit d5fa30d

Please sign in to comment.