1

I need a Delphi reversible Hashed ID function that is quick.

  • Short, obfuscated and efficient IDs
  • No collisions (at least up to 32-bit unsigned integer at least)
  • Reversible
  • Fast
  • preferably something that has an input Key, so it can be randomised a bit... otherwise, a '3' will always be 23zkJ5 on all my software modules.
  • works cross-platform

Something like Youtube's video identifier.

Encode(3); // => "23zkJ5"
Decode('23zkJ5'); // => 3

PHP seems to have quite a few of these; I can't find one for Delphi.

I look at this but not really what I wanted, plus I need something in Delphi.

Reversible hash function?

$generator->encode(6); // => "43Vht7"
$generator->decode('43Vht7'); // => 6

I need something like what PHP offers:

https://github.com/delight-im/PHP-IDs

I can't use MD5 as it's not reversible; using Lockbox encryption/decryption seems a bit over-kill? (if really no choice, which algorithm in Lockbox would be the best choice for this?)

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Peter Jones
  • 451
  • 2
  • 12
  • 4
    You are not looking for a hash, you are looking for encryption. There are plenty of Delphi source code for that. StackOverflow is not the place to ask recommendation for library or component. – fpiette May 13 '21 at 12:05
  • ok because this php one called it a 'reversible hash'... i tried a couple of encryption but it doesn't stick to alphanumeric characters... base64 after it makes it too long, so I'm just wondering if someone has a quick & simple routine. – Peter Jones May 13 '21 at 14:36
  • Good old XOR algorithm coupled with some permutations. This is perfectly reversible and easy to implement (and cracked of course). – fpiette May 13 '21 at 15:33
  • If you want to keep to the same character set then look at [Format Preserving Encryption](https://en.wikipedia.org/wiki/Format-preserving_encryption). That is a bit more of a hassle to get working, but should do what you want. – rossum May 13 '21 at 19:53
  • "_but it doesn't stick to alphanumeric characters_" - and so do hashes, so what's your point? If "_reversible hashes_" are possible then you could also tell me if my sum `42` came from the summands `15` and `27` or `-8` and `50`. However, sums can't be reversed to their summands. – AmigoJack May 13 '21 at 22:53
  • @AmigoJack What i mean is it has non-ascii (?) characters. – Peter Jones May 14 '21 at 01:05

1 Answers1

1

Use AES and convert the cypher byte array to a hex string or to Base64.

for a code example see here

AES encrypt string in Delphi (10 Seattle) with DCrypt, decrypt with PHP/OpenSSL

Stefan Fenn
  • 483
  • 5
  • 13
  • thanks for the reply. I'm just wondering how some routines can do it with such a short code, eg Youtube is 11 characters long and several PHP routines are even less. I Just need to cater for 32-bit number (4 billion + numbers). – Peter Jones May 14 '21 at 16:27