Skip to content

Commit df25965

Browse files
committed
Append random chars to uniqid
1 parent fe62ccf commit df25965

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

src/Database/Database.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,14 +1174,22 @@ protected function decodeAttribute(string $name, $value, Document $document)
11741174
}
11751175

11761176
/**
1177-
* Get Unique ID of varying length
1178-
*
1177+
* Get Unique ID
1178+
*
1179+
* @param int $padding extra random bytes to append to 13-char uniqid
1180+
*
11791181
* @return string
11801182
*/
1181-
public function getId(int $length = 13): string
1183+
public function getId(int $padding = 0): string
11821184
{
1183-
$bytes = \random_bytes(\ceil($length / 2)); // one byte expands to two chars
1184-
return \substr(\bin2hex($bytes), 0, $length);
1185+
$padding = '';
1186+
1187+
if ($padding > 0) {
1188+
$bytes = \random_bytes(\ceil($padding / 2)); // one byte expands to two chars
1189+
$padding = \substr(\bin2hex($bytes), 0, $padding);
1190+
}
1191+
1192+
return \uniqid() . $padding;
11851193
}
11861194

11871195
/**

0 commit comments

Comments
 (0)