-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpOID.php
127 lines (126 loc) · 3.46 KB
/
phpOID.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
// File name : phpOID.php
// Version : 1.1
// Begin : 26/03/2009
// Last Update : 05/01/2024
// Author : Hida - https://github.com/hidasw
// License : GPL-3.0 license
// -------------------------------------------------------------------
//
// Covert OID to hex and vice versa.
// limited to 10 digit oid
*/
class oid {
//============================================================+
// function : oid2hex
// Version : 1.1
// Begin : 26/03/2009
// Last Update : 30/04/2024
// Author : Hida - https://github.com/hidasw
// License : GPL-3.0 license
// Description : Convert oid number to hexadecimal form
// Changes : Tuesday, 30 April 2024 08:25:52 Simplified from 3 functions to just one function
// -------------------------------------------------------------------
public static function tohex($oid) {
if(!preg_match("~^(?!\.)[0-9.]*$(?<!\.)~", $oid)) { // only allow dot and number
return false;
}
$arr = explode(".", trim($oid, "."));
if(count($arr)<2) {
return false;
}
$i = 0;
$ret = false;
foreach($arr as $dec) {
if($i == 0) {
if($dec <= 2) {
$add = $dec*40;
} else {
return false;
}
}
if($i == 1) {
if($dec > 39) {
return false;
} else {
$ret = str_pad(dechex($dec+$add), 2, "0", STR_PAD_LEFT);
}
}
if($i > 1) {
if(strlen($dec)>10) { return false; } // max 10 digit
if($dec >= 128) {
$ix=0;
$hex = array();
while($dec != $dec%128) {
$hida = $dec%128;
if($ix != 0) { // not first loop
$hida = $hida+128;
}
$dec = floor($dec/128);
$hex[] = str_pad(dechex($hida), 2, "0", STR_PAD_LEFT);
if($dec == $dec%128) { // end loop
$hex[] = str_pad(dechex($dec+128), 2, "0", STR_PAD_LEFT);
}
$ix++;
}
$ret .= implode('', array_reverse($hex));
} else {
$ret .= str_pad(dechex($dec), 2, "0", STR_PAD_LEFT);
}
}
$i++;
}
return $ret;
}
//============================================================+
// function : oidfromhex
// Version : 1.1
// Begin : 26/03/2009
// Last Update : 30/04/2024
// Author : Hida - https://github.com/hidasw
// License : GPL-3.0 license
// Description : Convert hex to oid
// -------------------------------------------------------------------
public static function fromhex($hex) {
if(!ctype_xdigit($hex)) { return false; }
$split = str_split($hex, 2);
$i = 0;
$nex = false;
$result = false;
foreach($split as $val) {
$dec = hexdec($val);
if($i == 0) {
if($dec >= 128) {
$nex = (128*($dec-128))-80;
$result = "2.";
} elseif($dec >= 80) {
$first = $dec-80;
$result = "2.$first.";
} elseif($dec >= 40) {
$first = $dec-40;
$result = "1.$first.";
} elseif($dec < 40) {
$first = $dec-0;
$result = "0.$first.";
}
} else {
$mplx = ($dec-128)*128;
if($dec > 127) {
if($nex == false) {
$nex = $mplx;
} else {
$nex = ($nex*128)+$mplx;
}
} else {
$result .= ($dec+$nex).".";
if($dec <= 127) {
$nex = 0;
}
}
}
$i++;
}
return rtrim($result, ".");
}
?>