Rev 78 | Rev 80 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 78 | Rev 79 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * UUID utils for PHP |
4 | * UUID utils for PHP |
5 | * Copyright 2011 - 2023 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2011 - 2024 Daniel Marschall, ViaThinkSoft |
6 | * Version 2023-11-18 |
6 | * Version 2024-03-09 |
7 | * |
7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * you may not use this file except in compliance with the License. |
9 | * you may not use this file except in compliance with the License. |
10 | * You may obtain a copy of the License at |
10 | * You may obtain a copy of the License at |
11 | * |
11 | * |
Line 831... | Line 831... | ||
831 | case 3: |
831 | case 3: |
832 | echo sprintf("%-32s %s\n", "Variant:", "[0b111] Reserved for future use"); |
832 | echo sprintf("%-32s %s\n", "Variant:", "[0b111] Reserved for future use"); |
833 | break; |
833 | break; |
834 | } |
834 | } |
835 | 835 | ||
- | 836 | // START: HickelSOFT UUID |
|
- | 837 | ||
- | 838 | // Block 5 |
|
- | 839 | $signature = substr($uuid,20,12); |
|
- | 840 | if (strtolower($signature) == '4849434b454c'/*HICKEL*/) { |
|
- | 841 | // HickelSOFT "SQL Server sortable UUID in C#" |
|
- | 842 | // Version 2: Resolution of 1 milliseconds, random part of 18 bits, UTC time, UUIDv8 conform. |
|
- | 843 | // Example: 2088dc33-000d-8045-87e8-4849434b454c |
|
- | 844 | // Block 4 |
|
- | 845 | $rnd2bits = hexdec(substr($uuid,16,1)) & 0x3; |
|
- | 846 | $year = hexdec(substr($uuid,17,3)); |
|
- | 847 | // Block 3 |
|
- | 848 | $dayOfYear = hexdec(substr($uuid,13,3)); |
|
- | 849 | $day = intval(getDateFromDay($year, $dayOfYear)->format('d')); |
|
- | 850 | $month = intval(getDateFromDay($year, $dayOfYear)->format('m')); |
|
- | 851 | // Block 2 |
|
- | 852 | $minuteOfDay = hexdec(substr($uuid,8,4)); |
|
- | 853 | $minutes = $minuteOfDay % 60; |
|
- | 854 | $hours = (int)floor($minuteOfDay / 60); |
|
- | 855 | // Block 1 |
|
- | 856 | $rnd16bits = substr($uuid,0,4); |
|
- | 857 | $millisecond8bits = hexdec(substr($uuid,4,2)); |
|
- | 858 | $milliseconds = round($millisecond8bits / 255 * 999); |
|
- | 859 | $seconds = hexdec(substr($uuid,6,2)); |
|
- | 860 | // Verbose info |
|
- | 861 | echo "\n<u>Interpretation of <a href=\"https://gist.github.com/danielmarschall/7fafd270a3bc107d38e8449ce7420c25\">HickelSOFT \"SQL Server Sortable Custom UUID\", Version 2</a></u>\n\n"; |
|
- | 862 | echo sprintf("%-32s %s\n", "Random 16 bits:", "[0x$rnd16bits] 0b".str_pad("".base_convert($rnd16bits, 16, 2), 16, '0', STR_PAD_LEFT)); |
|
- | 863 | echo sprintf("%-32s %s\n", "Milliseconds:", "[0x".substr($uuid,4,2)."] $milliseconds"); |
|
- | 864 | echo sprintf("%-32s %s\n", "Seconds:", "[0x".substr($uuid,6,2)."] $seconds"); |
|
- | 865 | echo sprintf("%-32s %s\n", "Minute of day:", "[0x".substr($uuid,8,4)."] $minuteOfDay (".str_pad("$hours",2,'0',STR_PAD_LEFT).":".str_pad("$minutes",2,'0',STR_PAD_LEFT).")"); |
|
- | 866 | echo sprintf("%-32s %s\n", "Day of year:", "[0x".substr($uuid,13,3)."] $dayOfYear (Day=$day, Month=$month)"); |
|
- | 867 | echo sprintf("%-32s %s\n", "Random 2 bits:", "[$rnd2bits] 0b".str_pad("".base_convert("$rnd2bits", 16, 2), 2, '0', STR_PAD_LEFT)); |
|
- | 868 | echo sprintf("%-32s %s\n", "Year:", "[0x".substr($uuid,17,3)."] $year)"); |
|
- | 869 | echo sprintf("%-32s %s\n", "Signature:", "[0x".substr($uuid,20,12)."] HICKEL"); |
|
- | 870 | echo sprintf("%-32s %s\n", "UTC Date Time:", |
|
- | 871 | str_pad("$year",4,'0',STR_PAD_LEFT).'-'. |
|
- | 872 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
|
- | 873 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
|
- | 874 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
|
- | 875 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
|
- | 876 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
|
- | 877 | str_pad("$milliseconds",2,'0',STR_PAD_LEFT) |
|
- | 878 | ); |
|
- | 879 | echo sprintf("%-32s %s\n", "Implementation:", "https://gist.github.com/danielmarschall/7fafd270a3bc107d38e8449ce7420c25"); |
|
- | 880 | } else if (strtolower($signature) == '000000000000') { |
|
- | 881 | // HickelSOFT "SQL Server sortable UUID in C#" |
|
- | 882 | // Version 1: Resolution of 1 milliseconds, random part of 16 bits, local timezone, NOT UUIDv8 conform. |
|
- | 883 | // Example: ff38da51-1301-0903-2420-000000000000 |
|
- | 884 | // Block 4 |
|
- | 885 | $year = substr($uuid,18,2) . substr($uuid,16,2); |
|
- | 886 | if (!is_numeric($year) || ($year < 2000) || ($year > 2999)) $year = 'XXXX'; else $year = intval($year); |
|
- | 887 | // Block 3 |
|
- | 888 | $day = substr($uuid,12,2); |
|
- | 889 | if (!is_numeric($day) || ($day < 0) || ($day >= 60)) $day = 'XX'; else $day = intval($day); |
|
- | 890 | $month = substr($uuid,14,2); |
|
- | 891 | if (!is_numeric($month) || ($month < 0) || ($month >= 60)) $month = 'XX'; else $month = intval($month); |
|
- | 892 | // Block 2 |
|
- | 893 | $minutes = substr($uuid,8,2); |
|
- | 894 | if (!is_numeric($minutes) || ($minutes < 0) || ($minutes >= 60)) $minutes = 'XX'; else $minutes = intval($minutes); |
|
- | 895 | $hours = substr($uuid,10,2); |
|
- | 896 | if (!is_numeric($hours) || ($hours < 0) || ($hours >= 60)) $hours = 'XX'; else $hours = intval($hours); |
|
- | 897 | // Block 1 |
|
- | 898 | $rnd16bits = substr($uuid,0,4); |
|
- | 899 | $millisecond8bits = hexdec(substr($uuid,4,2)); |
|
- | 900 | $milliseconds = round($millisecond8bits / 255 * 999); |
|
- | 901 | $seconds = substr($uuid,6,2); |
|
- | 902 | if (!is_numeric($seconds) || ($seconds < 0) || ($seconds >= 60)) $seconds = 'XX'; else $seconds = intval($seconds); |
|
- | 903 | // Verbose info |
|
- | 904 | echo "\n<u>Interpretation of <a href=\"https://gist.github.com/danielmarschall/7fafd270a3bc107d38e8449ce7420c25\">HickelSOFT \"SQL Server Sortable Custom UUID\", Version 1</a></u>\n\n"; |
|
- | 905 | echo sprintf("%-32s %s\n", "Random 16 bits:", "[0x$rnd16bits] 0b".str_pad(base_convert($rnd16bits, 16, 2), 16, '0', STR_PAD_LEFT)); |
|
- | 906 | echo sprintf("%-32s %s\n", "Milliseconds:", "[0x".substr($uuid,4,2)."] $milliseconds"); |
|
- | 907 | echo sprintf("%-32s %s\n", "Seconds:", "[0x".substr($uuid,6,2)."] $seconds"); |
|
- | 908 | echo sprintf("%-32s %s\n", "Minutes:", "[0x".substr($uuid,8,2)."] $minutes"); |
|
- | 909 | echo sprintf("%-32s %s\n", "Hours:", "[0x".substr($uuid,10,2)."] $hours"); |
|
- | 910 | echo sprintf("%-32s %s\n", "Day:", "[0x".substr($uuid,12,2)."] $day"); |
|
- | 911 | echo sprintf("%-32s %s\n", "Month:", "[0x".substr($uuid,14,2)."] $month"); |
|
- | 912 | echo sprintf("%-32s %s\n", "Year:", "[0x".substr($uuid,16,4)."] $year"); |
|
- | 913 | echo sprintf("%-32s %s\n", "Signature:", "[0x".substr($uuid,20,12)."]"); |
|
- | 914 | echo sprintf("%-32s %s\n", "Generator's Local Date Time:", |
|
- | 915 | str_pad("$year",4,'0',STR_PAD_LEFT).'-'. |
|
- | 916 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
|
- | 917 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
|
- | 918 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
|
- | 919 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
|
- | 920 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
|
- | 921 | str_pad("$milliseconds",2,'0',STR_PAD_LEFT) |
|
- | 922 | ); |
|
- | 923 | echo sprintf("%-32s %s\n", "Implementation:", "https://gist.github.com/danielmarschall/7fafd270a3bc107d38e8449ce7420c25"); |
|
- | 924 | } |
|
- | 925 | ||
- | 926 | // END: HickelSOFT UUID |
|
- | 927 | ||
836 | if (!$echo) { |
928 | if (!$echo) { |
837 | $out = ob_get_contents(); |
929 | $out = ob_get_contents(); |
838 | ob_end_clean(); |
930 | ob_end_clean(); |
839 | return $out; |
931 | return $out; |
840 | } else { |
932 | } else { |
Line 869... | Line 961... | ||
869 | $hex = str_pad($hex, 32, "0", STR_PAD_LEFT); |
961 | $hex = str_pad($hex, 32, "0", STR_PAD_LEFT); |
870 | return substr($hex,0,8).'-'.substr($hex,8,4).'-'.substr($hex,12,4).'-'.substr($hex,16,4).'-'.substr($hex,20,12); |
962 | return substr($hex,0,8).'-'.substr($hex,8,4).'-'.substr($hex,12,4).'-'.substr($hex,16,4).'-'.substr($hex,20,12); |
871 | } else if ((count($ary) == 14) && (strpos($oid, '1.2.840.113556.1.8000.2554.') === 0)) { |
963 | } else if ((count($ary) == 14) && (strpos($oid, '1.2.840.113556.1.8000.2554.') === 0)) { |
872 | // Microsoft UUID-to-OID |
964 | // Microsoft UUID-to-OID |
873 | // Example: {53c08bb6-b2eb-5038-bf28-ad41a08c50ef} = 1.2.840.113556.1.8000.2554.21440.35766.45803.20536.48936.11354528.9195759 |
965 | // Example: {53c08bb6-b2eb-5038-bf28-ad41a08c50ef} = 1.2.840.113556.1.8000.2554.21440.35766.45803.20536.48936.11354528.9195759 |
874 | $a = $ary[7]; |
966 | $a = intval($ary[7]); |
875 | $b = $ary[8]; |
967 | $b = intval($ary[8]); |
876 | $c = $ary[9]; |
968 | $c = intval($ary[9]); |
877 | $d = $ary[10]; |
969 | $d = intval($ary[10]); |
878 | $e = $ary[11]; |
970 | $e = intval($ary[11]); |
879 | $f = $ary[12]; |
971 | $f = intval($ary[12]); |
880 | $g = $ary[13]; |
972 | $g = intval($ary[13]); |
881 | return dechex($a).dechex($b).'-'.dechex($c).'-'.dechex($d).'-'.dechex($e).'-'.dechex($f).dechex($g); |
973 | return dechex($a).dechex($b).'-'.dechex($c).'-'.dechex($d).'-'.dechex($e).'-'.dechex($f).dechex($g); |
882 | } else if ((count($ary) == 10) && (strpos($oid, '1.3.6.1.4.1.54392.1.') === 0)) { |
974 | } else if ((count($ary) == 10) && (strpos($oid, '1.3.6.1.4.1.54392.1.') === 0)) { |
883 | // Waterjuice UUID-to-OID 2x64 Bits |
975 | // Waterjuice UUID-to-OID 2x64 Bits |
884 | // Example: {53c08bb6-b2eb-5038-bf28-ad41a08c50ef} = 1.3.6.1.4.1.54392.1.6034977117478539320.13774449957690691823 |
976 | // Example: {53c08bb6-b2eb-5038-bf28-ad41a08c50ef} = 1.3.6.1.4.1.54392.1.6034977117478539320.13774449957690691823 |
885 | $a1 = gmp_strval(gmp_init($ary[8],10),16); if (strlen($a1)>16) return false; |
977 | $a1 = gmp_strval(gmp_init($ary[8],10),16); if (strlen($a1)>16) return false; |
Line 1494... | Line 1586... | ||
1494 | $sponge = SHA3::init(SHA3::SHAKE256); |
1586 | $sponge = SHA3::init(SHA3::SHAKE256); |
1495 | $sponge->absorb($msg); |
1587 | $sponge->absorb($msg); |
1496 | $bin = $sponge->squeeze($outputLength); |
1588 | $bin = $sponge->squeeze($outputLength); |
1497 | return $binary ? $bin : bin2hex($bin); |
1589 | return $binary ? $bin : bin2hex($bin); |
1498 | } |
1590 | } |
- | 1591 | ||
- | 1592 | /** |
|
- | 1593 | * Converts the day of year of a year into a DateTime object |
|
- | 1594 | * @param int $year The year |
|
- | 1595 | * @param int $dayOfYear The day of year (value 1 till 365 or 1 till 366 for leap years) |
|
- | 1596 | * @return \DateTime The resulting date |
|
- | 1597 | */ |
|
- | 1598 | function getDateFromDay(int $year, int $dayOfYear): \DateTime { |
|
- | 1599 | // Note: "Y z" and "z Y" make a difference for leap years (last tested with PHP 8.0.3) |
|
- | 1600 | $date = \DateTime::createFromFormat('Y z', strval($year) . ' ' . strval($dayOfYear-1)); |
|
- | 1601 | return $date; |
|
- | 1602 | } |
|
- | 1603 |