148 lines
3.6 KiB
PHP
148 lines
3.6 KiB
PHP
<?php
|
|
|
|
define('SECRET_KEY', 2704960011);
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| ENCRYPT FORMAT BARU
|
|
| MP-1234567890-001
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
function encryptNumberID($id, $dailyNumber = 1)
|
|
{
|
|
$salt = rand(100, 999);
|
|
|
|
$raw = ($id + $salt) * SECRET_KEY;
|
|
|
|
$middle = substr(
|
|
str_pad($raw, 12, '0', STR_PAD_LEFT),
|
|
0,
|
|
7
|
|
);
|
|
|
|
// 001, 002, dst
|
|
$dailyNumber = str_pad($dailyNumber, 3, '0', STR_PAD_LEFT);
|
|
|
|
return "MP-$salt$middle-$dailyNumber";
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| DECRYPT SUPPORT FORMAT LAMA & BARU
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| FORMAT LAMA
|
|
| MP-1234567890-54321
|
|
|
|
|
| FORMAT BARU
|
|
| MP-1234567890-001
|
|
|
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
function decryptNumberID($encrypted)
|
|
{
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| FORMAT LAMA (5 digit belakang)
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
if (preg_match('/^MP-(\d{10})-(\d{5})$/', $encrypted, $matches)) {
|
|
|
|
$salt = intval(substr($matches[1], 0, 3));
|
|
|
|
$middle = substr($matches[1], 3);
|
|
|
|
$checksum = $matches[2];
|
|
|
|
for ($id = 1; $id <= 100000; $id++) {
|
|
|
|
$raw = ($id + $salt) * SECRET_KEY;
|
|
|
|
$encoded = substr(
|
|
str_pad($raw, 12, '0', STR_PAD_LEFT),
|
|
0,
|
|
7
|
|
);
|
|
|
|
if ($encoded !== $middle) {
|
|
continue;
|
|
}
|
|
|
|
$check = substr(
|
|
strval(($id * 97 + $salt) % 100000),
|
|
-5
|
|
);
|
|
|
|
if ($check === $checksum) {
|
|
return $id;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| FORMAT BARU (3 digit belakang)
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
if (preg_match('/^MP-(\d{10})-(\d{3})$/', $encrypted, $matches)) {
|
|
|
|
$salt = intval(substr($matches[1], 0, 3));
|
|
|
|
$middle = substr($matches[1], 3);
|
|
|
|
for ($id = 1; $id <= 100000; $id++) {
|
|
|
|
$raw = ($id + $salt) * SECRET_KEY;
|
|
|
|
$encoded = substr(
|
|
str_pad($raw, 12, '0', STR_PAD_LEFT),
|
|
0,
|
|
7
|
|
);
|
|
|
|
if ($encoded === $middle) {
|
|
return $id;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// define('SECRET_KEY', 2704960011);
|
|
|
|
// function encryptNumberID($id) {
|
|
// $salt = rand(100, 999);
|
|
// $raw = ($id + $salt) * SECRET_KEY;
|
|
// $middle = substr(str_pad($raw, 12, '0', STR_PAD_LEFT), 0, 7);
|
|
// $checksum = substr(strval(($id * 97 + $salt) % 100000), -5);
|
|
|
|
// return "MP-$salt$middle-$checksum";
|
|
// }
|
|
|
|
// function decryptNumberID($encrypted) {
|
|
// if (!preg_match('/^MP-(\d{10})-(\d{5})$/', $encrypted, $matches)) {
|
|
// return null;
|
|
// }
|
|
|
|
// $salt = intval(substr($matches[1], 0, 3));
|
|
// $middle = substr($matches[1], 3);
|
|
// $checksum = $matches[2];
|
|
|
|
// $maxTries = 100000;
|
|
|
|
// for ($id = 1; $id <= $maxTries; $id++) {
|
|
// $raw = ($id + $salt) * SECRET_KEY;
|
|
// $encoded = substr(str_pad($raw, 12, '0', STR_PAD_LEFT), 0, 7);
|
|
// if ($encoded !== $middle) {
|
|
// continue;
|
|
// }
|
|
|
|
// $check = substr(strval(($id * 97 + $salt) % 100000), -5);
|
|
// if ($check === $checksum) {
|
|
// return $id; // ketemu
|
|
// }
|
|
// }
|
|
|
|
// return null;
|
|
// }
|