Last active
August 29, 2015 13:55
-
-
Save charlycoste/8755907 to your computer and use it in GitHub Desktop.
Génère une liste aléatoire de sha1, avec leurs alias, sans collision. Ce script peut être rejoué avec une limite supérieure (la variable $max) pour générer de nouveaux sha1 sans écraser les anciens. J'ai écrit ce script pour numéroter mes documents comptables de façon unique tout en pouvant y faire référence simplement par leur alias.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 | 71A2FE8DBBBD2B7C60083CFFC4714FAE6860D668 | |
---|---|---|
F7 | F70F565F1FD894CCD0FC9F9CD3BB327C83603A89 | |
89 | 899DF47D3C425BFA64FF0CB46139BA639FC1E348 | |
DF | DFD7BEE64B0882557415C95B803F0EBFC5D4E31C | |
22 | 22EE2B60CB7FEF3D6FFF9F2E7E81574DBCB52CC7 | |
B2 | B2270E7BFBCD9FCEF5DFDB5FABAB6694E1FF9BE8 | |
D9 | D99D4328DAA150B2D0C0846B0217BF0BADDBBAB2 | |
39 | 39BA9D49DBFF3C56A3E3FEFC9B89BF8F4A07D084 | |
0C | 0C1812FC972EE469A6822DAEB8E43626ADBD5297 | |
08 | 086C7549AC945C681A4848C6CD679998D8B76B78 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Retourne l'alias le plus court possible d'un hash, en fonction des alias déjà connus | |
*/ | |
function alias($hash, $history) { | |
// Taille minimum d'un alias | |
$i = 2; | |
do { | |
$candidate = substr($hash, 0, $i++); | |
} while (in_array($candidate, $history)); | |
return $candidate; | |
} | |
/** | |
* Nombre de hash voulus | |
*/ | |
$max = 10; | |
/** | |
* Fichier utilisé pour stocker le résultat | |
*/ | |
$csv = 'sha1.csv'; | |
/** | |
* variable globale contenant les hash comme clée et les alias comme valeur | |
* exemple: $hashes = array("086C7549AC945C681A4848C6CD679998D8B76B78" => "08"); | |
*/ | |
$hashes = array(); | |
/** | |
* Récupère les hash déjà générés | |
*/ | |
if (($handle = fopen($csv, "r")) !== FALSE) { | |
while (($data = fgetcsv($handle, 1000)) !== FALSE) { | |
$hashes[$data[1]] = $data[0]; | |
} | |
fclose($handle); | |
} | |
/** | |
* Complète la liste jusqu'à la limite définie dans $max | |
*/ | |
for ($i=count($hashes); $i<$max;) { | |
$new_hash = strtoupper(sha1(rand().microtime())); | |
if (!in_array($new_hash, $hashes)) { | |
$hashes[$new_hash] = alias($new_hash, $hashes); | |
$i++; | |
} | |
} | |
/** | |
* Ecrit le résultat dans le fichier | |
*/ | |
if (($handle = fopen($csv, "w")) !== FALSE) { | |
foreach ($hashes as $hash=>$alias) { | |
fputcsv($handle, array($alias, $hash)); | |
} | |
fclose($handle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ruby version: https://gist.github.com/ericsagnes/8762036