3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 * Solaris has thread-safe crypt()
13 * Linux has crypt_r(); "struct crypt_data" is more than 128K
14 * FreeBSD needs the mutex to protect crypt()
17 * ngx_crypt_init() to init mutex
23 #if (NGX_HAVE_GNU_CRYPT_R)
26 ngx_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
36 /* work around the glibc bug */
37 cd.current_salt[0] = ~salt[0];
39 value = crypt_r((char *) key, (char *) salt, &cd);
44 len = ngx_strlen(value);
46 *encrypted = ngx_pnalloc(pool, len);
48 ngx_memcpy(*encrypted, value, len + 1);
53 ngx_log_error(NGX_LOG_CRIT, pool->log, err, "crypt_r() failed");
61 ngx_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
67 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
69 /* crypt() is a time consuming funtion, so we only try to lock */
71 if (ngx_mutex_trylock(ngx_crypt_mutex) != NGX_OK) {
79 value = crypt((char *) key, (char *) salt);
82 len = ngx_strlen(value);
84 *encrypted = ngx_pnalloc(pool, len);
86 ngx_memcpy(*encrypted, value, len + 1);
89 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
90 ngx_mutex_unlock(ngx_crypt_mutex);
97 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
98 ngx_mutex_unlock(ngx_crypt_mutex);
101 ngx_log_error(NGX_LOG_CRIT, pool->log, err, "crypt() failed");
108 #endif /* NGX_CRYPT */