Skip to content

Instantly share code, notes, and snippets.

@KEINOS
Last active January 21, 2020 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KEINOS/232447428723e652aca0e104a01a6554 to your computer and use it in GitHub Desktop.
Save KEINOS/232447428723e652aca0e104a01a6554 to your computer and use it in GitHub Desktop.
UTF-8文字コードを利用したN進数変換関数。MD5ハッシュを10桁の文字列に圧縮できます。詳細 https://blog.keinos.com/20170720_2810
<?php
// Include user function
include('2-mb_base_encode.php');
// Input string
$s = 'This is a sample string to be hashed.';
// Calculate samples
$result1 = $s;
$result2 = md5($s);
$result3 = mb_base_encode(hexdec(md5($s)));
// Output
echo2($result1); // 'This is a sample string to be hashed.'
echo2($result2); // 'baad33e1e97f316b9750c27c86bf64d6'
echo2($result3); // '䙔倁屩劷䋾彨䏔䂌䤘剒'
// etc
/**
* Just echoes string with PHP_EOL
*
* @param string
* return string
*/
function echo2($s){
echo $s . PHP_EOL;
}
?>
<?php
/**
* Returns base Nth encoded string from decimal number input.
*
* @param integer $number Decimal number to encode
* @return string Encoded string
*/
function mb_base_encode($number)
{
$char = array_merge(
mb_range('䀀', '䶵'), // U+4000 - U+4DB5
mb_range('一', '俿'), // U+4E00 - U+4FFF
mb_range('倀', '忿') // U+5000 - U+5FFF
// REF: http://www.utf8-chartable.com/unicode-utf8-table.pl?start=16384&number=1024
);
$base = count($char);
$result = "";
while ($number > 0) {
$result = $char[ fmod($number, $base) ] . $result;
$number = floor($number / $base);
}
return ( $result == "" ) ? 0 : $result;
}
/* ---------------------------------------------------------------------
This function requires mb_range.
https://gist.github.com/rodneyrehm/1306118
--------------------------------------------------------------------- */
/**
* multibyte string compatible range('A', 'Z')
*
* @param string $start Character to start from (included)
* @param string $end Character to end with (included)
* @return array list of characters in unicode alphabet from $start to $end
* @author Rodney Rehm
*/
function mb_range($start, $end) {
// if start and end are the same, well, there's nothing to do
if ($start == $end) {
return array($start);
}
$_result = array();
// get unicodes of start and end
list(, $_start, $_end) = unpack("N*", mb_convert_encoding($start . $end, "UTF-32BE", "UTF-8"));
// determine movement direction
$_offset = $_start < $_end ? 1 : -1;
$_current = $_start;
while ($_current != $_end) {
$_result[] = mb_convert_encoding(pack("N*", $_current), "UTF-8", "UTF-32BE");
$_current += $_offset;
}
$_result[] = $end;
return $_result;
}
?>
<?php
function mb_md5(string $string, array $chars)
{
return mb_base_encode(hexdec(md5($string)), $chars);
}
function getCharsToUse(): array
{
return array_merge(
mb_range('䀀', '䶵'), // U+4000 - U+4DB5
mb_range('一', '俿'), // U+4E00 - U+4FFF
mb_range('倀', '忿') // U+5000 - U+5FFF
// REF: http://www.utf8-chartable.com/unicode-utf8-table.pl?start=16384&number=1024
);
}
/**
* Returns base Nth encoded string from decimal number input.
*/
function mb_base_encode(float $number, array $chars): string
{
$number = (double) $number;
$base = count($chars);
$result = "";
while ($number > 0) {
$result = $chars[ fmod($number, $base) ] . $result;
$number = floor($number / $base);
}
return ( $result == "" ) ? "0" : $result;
}
/**
* multibyte string compatible range('A', 'Z').
*
* @param string $start Character to start from (included)
* @param string $end Character to end with (included)
* @return array List of characters in unicode alphabet from $start to $end
* @note This function requires mb_range.
* @ref https://gist.github.com/rodneyrehm/1306118
*/
function mb_range(string $start, string $end): array
{
// if start and end are the same, well, there's nothing to do
if ($start == $end) {
return array($start);
}
$_result = array();
// get unicodes of start and end
list(, $_start, $_end) = unpack("N*", mb_convert_encoding($start . $end, "UTF-32BE", "UTF-8"));
// determine movement direction
$_offset = $_start < $_end ? 1 : -1;
$_current = $_start;
while ($_current != $_end) {
$_result[] = mb_convert_encoding(pack("N*", $_current), "UTF-8", "UTF-32BE");
$_current += $_offset;
}
$_result[] = $end;
return $_result;
}
<?php
// Include user function
include('functions.php');
// Input string
$s = 'This is a sample string to be hashed.';
// Characters to use
$chars = getCharsToUse();
echoEOL('Number of characters', count($chars));
// Calculate samples
$result1 = $s;
$result2 = md5($s);
$result3 = mb_base_encode(hexdec(md5($s)), $chars);
$result4 = mb_md5($s, $chars);
$result5 = mb_base_encode(hexdec('baad33e1e97f316b9750c27c86bf64d7'), $chars);
// Output
echoEOL('Original String', $result1); // 'This is a sample string to be hashed.'
echoEOL('Pure MD5 hashed string', $result2); // 'baad33e1e97f316b9750c27c86bf64d6'
echoEOL('Multibyte base encoded', $result3); // '䙔倁屩劷䋾彨䏔䂌䤘剒'
echoEOL('Same above but alies', $result4); // '䙔倁屩劷䋾彨䏔䂌䤘剒'
echoEOL('Little change value', $result5);
// etc
function echoEOL(string $title, string $s)
{
echo "${title}:\t${s}" . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment