Skip to content
Permalink
Browse files

Hashids minLength and alphabet option

  • Loading branch information...
Joris Vaesen
Joris Vaesen committed Jul 24, 2017
1 parent 817a2af commit 1e355528c39fc83c11aa324baf1e8b0c1033d4ce
@@ -72,9 +72,11 @@ use Muffin\Obfuscate\Model\Behavior\Strategy\HashIdStrategy;
$this->addBehavior('Muffin/Obfuscate.Obfuscate', [
// Strategy constructor parameter:
// $salt - Random alpha numeric string. You can also set "Obfuscate.salt"
// $minLength (optional) - The minimum hash length. Default: 0
// $alphabet (optional) - Custom alphabet to generate hash from. Default: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
// config instead of passing salt to construction.
// DO NOT USE same salt as set for "Security.salt" config.
'strategy' => new HashIdStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B')
'strategy' => new HashIdStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B', 10, 'abcdefghijklmnopqrstuvwxyz')
]);
```

@@ -138,9 +138,11 @@ public function findObfuscate(Query $query, array $options)
return $results->map(function ($row) {
$pk = $this->_table->primaryKey();
$row[$pk] = $this->obfuscate($row[$pk]);
return $row;
});
});
return $query;
}
@@ -21,12 +21,29 @@ class HashidStrategy implements StrategyInterface
*/
protected $_salt;
/**
* The minimum hash length.
*
* @var int
*/
protected $_minLength;
/**
* Custom alphabet to use.
*
* @var string
*/
protected $_alphabet;
/**
* Constructor.
*
* @param string $salt Random alpha-numeric set.
* @param int $minLength The minimum hash length.
* @param string $alphabet Custom alphabet to use.
* @throws \Exception
*/
public function __construct($salt = null)
public function __construct($salt = null, $minLength = 0, $alphabet = null)
{
if ($salt === null) {
$salt = Configure::read('Obfuscate.salt');
@@ -35,7 +52,14 @@ public function __construct($salt = null)
throw new \Exception('Missing salt for Hashid strategy');
}
$this->_salt = $salt;
$this->_hashid = new Hashids($salt);
$this->_minLength = $minLength;
$this->_alphabet = $alphabet;
if (!is_string($alphabet)) {
$this->_hashid = new Hashids($salt, $minLength);
} else {
$this->_hashid = new Hashids($salt, $minLength, $alphabet);
}
}
/**
@@ -53,7 +53,6 @@ public function setUp()
'through' => TableRegistry::get('Muffin/Obfuscate.ArticlesTags', ['table' => 'obfuscate_articles_tags'])
]);
$this->Obfuscate = $this->Articles->behaviors()->Obfuscate;
}
@@ -20,6 +20,20 @@ public function testElucidate()
$this->assertEquals(1, $this->strategy->elucidate('k8'));
}
public function testMinLength()
{
$this->strategy = new HashidStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B', 10);
$this->assertEquals('qxPAk8pnOV', $this->strategy->obfuscate(1));
}
public function testCustomAlphabet()
{
$this->strategy = new HashidStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B', 0, 'abcdefghijklmnopqrstuvwxyz');
$this->assertEquals('vg', $this->strategy->obfuscate(1));
}
/**
* @expectedException Exception
* @expectedExceptionMessage Missing salt for Hashid strategy

0 comments on commit 1e35552

Please sign in to comment.
You can’t perform that action at this time.