33 static u_char *ngx_crypt_to64(u_char *p, uint32_t v,
size_t n);
39 if (
ngx_strncmp(salt,
"$apr1$",
sizeof(
"$apr1$") - 1) == 0) {
40 return ngx_crypt_apr1(pool, key, salt, encrypted);
42 }
else if (
ngx_strncmp(salt,
"{PLAIN}",
sizeof(
"{PLAIN}") - 1) == 0) {
43 return ngx_crypt_plain(pool, key, salt, encrypted);
46 }
else if (
ngx_strncmp(salt,
"{SSHA}",
sizeof(
"{SSHA}") - 1) == 0) {
47 return ngx_crypt_ssha(pool, key, salt, encrypted);
49 }
else if (
ngx_strncmp(salt,
"{SHA}",
sizeof(
"{SHA}") - 1) == 0) {
50 return ngx_crypt_sha(pool, key, salt, encrypted);
61 ngx_crypt_apr1(
ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
65 u_char *p, *last,
final[16];
66 size_t saltlen, keylen;
75 salt +=
sizeof(
"$apr1$") - 1;
77 for (p = salt; *p && *p !=
'$' && p < last; p++) { }
93 for (n = keylen; n > 0; n -= 16) {
99 for (i = keylen;
i; i >>= 1) {
110 for (i = 0; i < 1000; i++) {
140 *encrypted =
ngx_pnalloc(pool,
sizeof(
"$apr1$") - 1 + saltlen + 16 + 1);
141 if (*encrypted == NULL) {
145 p =
ngx_cpymem(*encrypted,
"$apr1$",
sizeof(
"$apr1$") - 1);
149 p = ngx_crypt_to64(p, (
final[ 0]<<16) | (
final[ 6]<<8) |
final[12], 4);
150 p = ngx_crypt_to64(p, (
final[ 1]<<16) | (
final[ 7]<<8) |
final[13], 4);
151 p = ngx_crypt_to64(p, (
final[ 2]<<16) | (
final[ 8]<<8) |
final[14], 4);
152 p = ngx_crypt_to64(p, (
final[ 3]<<16) | (
final[ 9]<<8) |
final[15], 4);
153 p = ngx_crypt_to64(p, (
final[ 4]<<16) | (
final[10]<<8) |
final[ 5], 4);
154 p = ngx_crypt_to64(p,
final[11], 2);
162 ngx_crypt_to64(u_char *p, uint32_t v,
size_t n)
164 static u_char itoa64[] =
165 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
168 *p++ = itoa64[v & 0x3f];
177 ngx_crypt_plain(
ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
184 *encrypted =
ngx_pnalloc(pool,
sizeof(
"{PLAIN}") - 1 + len + 1);
185 if (*encrypted == NULL) {
189 p =
ngx_cpymem(*encrypted,
"{PLAIN}",
sizeof(
"{PLAIN}") - 1);
199 ngx_crypt_ssha(
ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
210 encoded.
data = salt +
sizeof(
"{SSHA}") - 1;
216 if (decoded.
data == NULL) {
238 if (*encrypted == NULL) {
242 encoded.
data =
ngx_cpymem(*encrypted,
"{SSHA}",
sizeof(
"{SSHA}") - 1);
244 encoded.
data[encoded.
len] =
'\0';
251 ngx_crypt_sha(
ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
260 decoded.
len =
sizeof(digest);
261 decoded.
data = digest;
270 if (*encrypted == NULL) {
274 encoded.
data =
ngx_cpymem(*encrypted,
"{SHA}",
sizeof(
"{SHA}") - 1);
276 encoded.
data[encoded.
len] =
'\0';