简体   繁体   中英

Generate a unique PIN code based on the phone number

With a unique phone number like 0241194000 , I want to generate a PIN code based on the phone number and also a way to check or validate that the PIN was really generated from the phone number.

EXAMPLE

Number  : 0241194000                      LENGHT(10)
PIN     : 675436                          LENGHT(6)     ONLY NUMERIC
Checker : 673AA3738SHZXCVDER              ANY LENGTH    ALPHANUMERIC.

Any links or help will be great.

What you are looking for a mathematical bijective function (preferably a complex one) that allows any Number (x) to be turned into a PIN, by means of said function

F(Number) = PIN

By having an bijective function, you can validate PINs by solving the equation in the opposite direction.

http://en.wikipedia.org/wiki/Bijection

For example:

Given the function: F(Number) = Number*2

function GeneratePIN(Number)
    return Number*2
end

function validatePIN(PIN,Number)
    return PIN == Number*2
end

Notwithstanding the correct comments above about that you can not create a unique PIN that is shorter than its source set (it boils down to hashing, which is by definition never unique), I'm assuming you mean a "code that cannot be reproduced for the phone number by an outsider, and that, given the phone number and the PIN, can be proven to be related, while accepting that the same PIN might also be valid when used with another phone number".

Assuming that, the easiest solution is to create a salted hash from the phone number. Sample pseudocode:

static uniqueHash = '9t45uufg92dit093ik,96igm0v9m6i09im09i309disl54923';
function createPinFromPhone(string phonenumber)
{
  string pin = '';
  do {
    hash = md5(phonenumber+uniqueHash);
    pin += extractNumbersFromString(hash);
    phonenumber = pin+hash;
  }
  while(pin.length < 6)
  return pin.subString(0, 6);
}

This is a (rough) example of a function that will always return the same pin code from the same phone number, and through the use of the unique secret key can never be reproduced by an outsider. Theoretically you could have an entropy problem, but not on this scale realistically.

If you just want to create a PIN from a phone number (where the phone number is unique and PIN is not necessarily unique), you can use one of the many hashing functions, such as CRC32, MD5, SHA1, ... and take just the number of bytes/numbers you need.

Note that it is not simple to make it secure (if you want that) since hashing functions are usually only making it harder to figure out the original value (in your case figuring out the number from the PIN) and not vice versa.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM