123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #include "libbb.h"
- static int i64c(int i)
- {
- i &= 0x3f;
- if (i == 0)
- return '.';
- if (i == 1)
- return '/';
- if (i < 12)
- return ('0' - 2 + i);
- if (i < 38)
- return ('A' - 12 + i);
- return ('a' - 38 + i);
- }
- int FAST_FUNC crypt_make_salt(char *p, int cnt )
- {
-
- unsigned x = getpid() + monotonic_us();
- do {
-
- x = x*1664525 + 1013904223;
-
- *p++ = i64c(x >> 16);
- *p++ = i64c(x >> 22);
- } while (--cnt);
- *p = '\0';
- return x;
- }
- char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
- {
- int len = 2/2;
- char *salt_ptr = salt;
-
- if ((algo[0]|0x20) != 'd') {
- len = 8/2;
- *salt_ptr++ = '$';
- *salt_ptr++ = '1';
- *salt_ptr++ = '$';
- #if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
- if ((algo[0]|0x20) == 's') {
- salt[1] = '5' + (strcasecmp(algo, "sha512") == 0);
- len = 16/2;
- }
- #endif
- }
- crypt_make_salt(salt_ptr, len);
- return salt_ptr;
- }
- #if ENABLE_USE_BB_CRYPT
- static char*
- to64(char *s, unsigned v, int n)
- {
- while (--n >= 0) {
-
- *s++ = i64c(v);
- v >>= 6;
- }
- return s;
- }
- #include "pw_encrypt_des.c"
- #include "pw_encrypt_md5.c"
- #if ENABLE_USE_BB_CRYPT_SHA
- #include "pw_encrypt_sha.c"
- #endif
- static struct const_des_ctx *des_cctx;
- static struct des_ctx *des_ctx;
- static char *my_crypt(const char *key, const char *salt)
- {
-
- if (salt[0] == '$' && salt[1] && salt[2] == '$') {
- if (salt[1] == '1')
- return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
- #if ENABLE_USE_BB_CRYPT_SHA
- if (salt[1] == '5' || salt[1] == '6')
- return sha_crypt((char*)key, (char*)salt);
- #endif
- }
- if (!des_cctx)
- des_cctx = const_des_init();
- des_ctx = des_init(des_ctx, des_cctx);
- return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
- }
- static void my_crypt_cleanup(void)
- {
- free(des_cctx);
- free(des_ctx);
- des_cctx = NULL;
- des_ctx = NULL;
- }
- char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
- {
- char *encrypted;
- encrypted = my_crypt(clear, salt);
- if (cleanup)
- my_crypt_cleanup();
- return encrypted;
- }
- #else
- char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
- {
- char *s;
- s = crypt(clear, salt);
-
- return xstrdup(s ? s : "");
- }
- #endif
|