# Fixed size output hash from varying input

To begin with I need to say I have no experience in cryptography (I'm a software developer) and found this place trying to figure out how to solve this, Here is the problem I need to solve in my job:

• Input: an integer varying from 1 up to 8 digits (i.e. 12345678, 2352, 76543)
• Output: a fixed size 6 digits code that could contain any alphanumeric and symbols (a-z, A-Z, 0-9, !@#$%*()|-_=+^/?) • The operation must be reversible, so given a code it should decode back to the original integer • There's no need for the output code to be cryptographicaly safe From my research I think I need some encryption algorithm to achieve this, but I couldn't find one that allow me to set the output size. Is it possible to be done? I need to implement this code in NodeJS so any lib that could handle this job is appreciated • Welcome to Cryptgraophy.SE. Encryption must be reversible, what you describe loses information. Hash ( you had tagged) is not reversible. In your case, the input space small, so it is easy to search the input. But, if you trim the result, then there will be collisions. May 24 at 20:39 • @kelalaka thanks for pointing this out. As I mentioned, I have no experience in cryptography so I might have committed some mistaking in setting the tags. Any tips in how to solve this problem is appreciated May 24 at 20:57 • Do you even need cryptography? 8 decimal digits fits easily within a single 32-bit integer. If there's no secret "key" value and it's reversible, you probably just want an encoding. May 24 at 21:29 • @SAIPeregrinus I think it's the case, there's no need for password, the only requirements is the output to be 6 digits long, and be reversible to original number. Unfortunately I'm not having any success in finding some May 24 at 23:02 • You have 78 characters. You need an encoding from 32 bits to 6 characters with an alphabet of 78. That's definitely possible,$78P6>2^{32}$, but I'm not aware of an existing scheme that will work perfectly for you (Base64 and Base32 both pad their outputs, etc). It shouldn't be hard to design one though. May 25 at 1:34 ## 1 Answer Use coLaps() to collapse numbers up to 99,999,999 into 6-character strings. Use exLaps() to perform the inverse of the compression done by coLaps(). Be prepared to explain how it works should you turn it in as homework. Here’s a hint: hexadecimal is base 16; think base 22. char* coLaps(unsigned int num) { //returns 6-character string static char dest[]=" A"; //5 spaces before A char *cp=dest+5; int q,r; if(num>99999999)return ">badNum"; q=num;while (q) { r=q%22;q/=22; *cp--=65+r; } return dest; } int exLaps(char *str) { // converts strings created by coLaps back to ints char ch; int num = 0; while (ch=(*str++)) { if(ch<60)continue; num*=22;num+=ch-65; } return num; }  • Addition:$22$was chosen because$22^6\ge10^8$and$21^6<10^8$; and the alphabet includes$26\ge22\$ letters. Off-topic note: returning a pointer to a static char array comes with danger: previous results are overwritten when new values are computed. Side remark: it's asked for a NodeJS solution. I tend to use tio.run for code samples.
– fgrieu
Jul 9 at 8:16
• I thought about the static variable bug. If this question was a “please do my homework” request I felt the bug was not a problem. The C code is not NodeJS. But it does show how to convert to non power-of-2 bases. I don’t speak NodeJS. Mostly I speak C/C++ and multiple assembly languages. Jul 10 at 19:00