Skip to content

Commit

Permalink
The uuid should be in hex.
Browse files Browse the repository at this point in the history
Converted the reference to hex, reference can only be 8 chars now
  • Loading branch information
Andy Pieters committed Mar 23, 2018
1 parent 66da8d0 commit 7d1c409
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion samples/DynamicUUID/decode.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require_once "../../vendor/autoload.php";

$uuid = 'sl49b880-1234-1234-inv2-018000178532';
$uuid = 'b0898a33-1234-1234-0000-494e56303031';
$secret = 'abcdef1234567890abcdef1234567890abcdef12';

try {
Expand Down
2 changes: 1 addition & 1 deletion samples/DynamicUUID/encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
$UUID = \Paynl\DynamicUUID::encode(
'SL-1234-1234',
'abcdef1234567890abcdef1234567890abcdef12',
'INV2018000178532'
'INV001'
);

echo $UUID;
21 changes: 16 additions & 5 deletions src/DynamicUUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@

class DynamicUUID
{
const REFERENCE_TYPE_STRING = 1;
const REFERENCE_TYPE_HEX = 0;

/**
* Generate a UUID
*
* @param string $serviceId
* @param string $secret
* @param string $reference Your reference to the transaction
* @param string $padChar The reference will be padded with this character, default '0'
* @param int $referenceType Define if you are using a string (8 chars) of hex (16 chars)
*
* @return string The UUID
*/
public static function encode($serviceId, $secret, $reference, $padChar = '0')
public static function encode($serviceId, $secret, $reference, $padChar = '0', $referenceType=self::REFERENCE_TYPE_STRING)
{
if($referenceType === self::REFERENCE_TYPE_STRING){
$reference = implode(unpack("H*", $reference));
}
self::validateSecret($secret);
self::validateServiceId($serviceId);
self::validateReference($reference);
Expand All @@ -30,7 +37,7 @@ public static function encode($serviceId, $secret, $reference, $padChar = '0')

$hash = hash_hmac('sha256', $UUIDData, $secret);

$UUID = "sl" . substr($hash, 0, 6) . $UUIDData;
$UUID = "b" . substr($hash, 0, 7) . $UUIDData;

return sprintf('%08s-%04s-%04s-%04s-%12s',
substr($UUID, 0, 8),
Expand All @@ -56,7 +63,7 @@ private static function validateServiceId($strServiceId)

private static function validateReference($strReference)
{
if ( ! preg_match('/^[a-z0-9]{0,16}$/i', $strReference)) {
if ( ! preg_match('/^[0-9a-f]{0,16}$/i', $strReference)) {
throw new Error('Invalid reference: only alphanumeric chars are allowed, up to 16 chars long');
}
}
Expand All @@ -78,7 +85,7 @@ private static function validatePadChar($strPadChar)
* @return array Array with serviceId and reference
* @throws Error
*/
public static function decode($uuid, $secret = null, $padChar = '0')
public static function decode($uuid, $secret = null, $padChar = '0', $referenceType = self::REFERENCE_TYPE_STRING)
{
if ( isset($secret)) {
self::validateSecret($secret);
Expand All @@ -95,6 +102,10 @@ public static function decode($uuid, $secret = null, $padChar = '0')
$reference = substr($uuidData, 8);

$reference = ltrim($reference, $padChar);

if($referenceType == self::REFERENCE_TYPE_STRING){
$reference = pack("H*", $reference);
}
return array(
'serviceId' => $serviceId,
'reference' => $reference
Expand All @@ -115,7 +126,7 @@ public static function validate($uuid, $secret)
$uuidData = substr($uuidData, 8);

$hash = hash_hmac('sha256', $uuidData, $secret);
$checksum = "sl" . substr($hash, 0, 6);
$checksum = "b" . substr($hash, 0, 7);

return $checksum == substr($uuid, 0, 8);
}
Expand Down

0 comments on commit 7d1c409

Please sign in to comment.