/* * Written by Peter Bex in 2011 and placed in the public domain. */ unsigned char _crypt_itoa64[64 + 1] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; #ifndef MIN #define MIN(a,b) ((a)<(b)?(a):(b)) #endif #ifndef MAX #define MAX(a,b) ((a)>(b)?(a):(b)) #endif void bytes_to_saltstring(unsigned char *bytes, unsigned int bytescount, char *salt, unsigned int saltlen) { int i, tmp, p; for (i = 0, p = 0; i < saltlen, p < bytescount; ++i,++salt) { switch(i%4) { case 0: /* Starting 6 bits of an octet */ *salt = _crypt_itoa64[(bytes[p] >> 2) & 0x3f]; break; case 1: /* Last 2 bits of an octet and the next one's first 4 */ tmp = bytes[p++] << 4; if (p < bytescount) tmp |= bytes[p] >> 4; *salt = _crypt_itoa64[tmp & 0x3f]; break; case 2: /* Last 4 bits of an octet and the next one's first 2 */ tmp = bytes[p++] << 2; if (p < bytescount) tmp |= bytes[p] >> 6; *salt = _crypt_itoa64[tmp & 0x3f]; break; case 3: /* Final 6 bits of an octet */ *salt = _crypt_itoa64[bytes[p++] & 0x3f]; break; } } }