Crates.io | optimus |
lib.rs | optimus |
version | 0.2.1 |
source | src |
created_at | 2023-03-13 06:22:36.966131 |
updated_at | 2023-03-13 16:06:14.511343 |
description | ID hashing and Obfuscation using Knuth's Algorithm |
homepage | |
repository | https://github.com/sampaioletti/optimus-rs |
max_upload_size | |
id | 808612 |
size | 14,791 |
With this library, you can transform your internal id's to obfuscated integers based on Knuth's integer hash. It is similar to Hashids, but will generate integers instead of random strings.
To get started you will need 3 things;
2147483647
(PRIME * INVERSE) & MAXID == 1
2147483647
use optimus::Optimus;
pub fn main() {
let prime = 1580030173; //choose this, but make sure its prime
let mod_inverse = 59260789; //calculate from prime or use Optimus::new_calculated
let random = 1163945558; //choose this at random
let opt = Optimus::new(prime, mod_inverse, random).unwrap();
//or
// let opt = Optimus::new_calculated(prime, random).unwrap();
// to calculate mod_inverse
let id = 15; //ID we want to obfuscate
let encoded_id = opt.encode(id); //obfuscated id=1103647397
assert_eq!(encoded_id, 1103647397);
let decoded_id = opt.decode(encoded_id); //back to 15
assert_eq!(id, decoded_id, "Id and decoded ID should be the same")
}
This package is based on the Go library by pjebs.
Which is in turn based on the PHP library by jenssegers.