Skip to content
Browse files

Added SHA-256 and SHA-512 support

  • Loading branch information...
1 parent 8d271b1 commit 468725b3dee3baef11e9a2aafe46e941ab50c236 Valery Kholodkov committed Nov 30, 2011
Showing with 167 additions and 0 deletions.
  1. +3 −0 Changelog
  2. +164 −0 ngx_http_upload_module.c
View
3 Changelog
@@ -1,4 +1,7 @@
+Version 2.2.1
+ * Added feature: SHA-256 and SHA-512
+
Version 2.2.0
* Added feature: resumable uploads
* Added feature: allow to use of $variables in "upload_pass" directive (Piotr Sikora)
View
164 ngx_http_upload_module.c
@@ -154,6 +154,8 @@ typedef struct {
unsigned int md5:1;
unsigned int sha1:1;
+ unsigned int sha256:1;
+ unsigned int sha512:1;
unsigned int crc32:1;
} ngx_http_upload_loc_conf_t;
@@ -167,6 +169,16 @@ typedef struct ngx_http_upload_sha1_ctx_s {
u_char sha1_digest[SHA_DIGEST_LENGTH * 2];
} ngx_http_upload_sha1_ctx_t;
+typedef struct ngx_http_upload_sha256_ctx_s {
+ SHA256_CTX sha256;
+ u_char sha256_digest[SHA256_DIGEST_LENGTH * 2];
+} ngx_http_upload_sha256_ctx_t;
+
+typedef struct ngx_http_upload_sha512_ctx_s {
+ SHA512_CTX sha512;
+ u_char sha512_digest[SHA512_DIGEST_LENGTH * 2];
+} ngx_http_upload_sha512_ctx_t;
+
struct ngx_http_upload_ctx_s;
/*
@@ -229,6 +241,8 @@ typedef struct ngx_http_upload_ctx_s {
ngx_http_upload_md5_ctx_t *md5_ctx;
ngx_http_upload_sha1_ctx_t *sha1_ctx;
+ ngx_http_upload_sha256_ctx_t *sha256_ctx;
+ ngx_http_upload_sha512_ctx_t *sha512_ctx;
uint32_t crc32;
unsigned int first_part:1;
@@ -259,6 +273,10 @@ static ngx_int_t ngx_http_upload_md5_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_upload_sha1_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_upload_sha256_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_upload_sha512_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_upload_file_size_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static void ngx_http_upload_content_range_variable_set(ngx_http_request_t *r,
@@ -693,6 +711,22 @@ static ngx_http_variable_t ngx_http_upload_aggregate_variables[] = { /* {{{ */
(uintptr_t) "0123456789ABCDEF",
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
+ { ngx_string("upload_file_sha256"), NULL, ngx_http_upload_sha256_variable,
+ (uintptr_t) "0123456789abcdef",
+ NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
+
+ { ngx_string("upload_file_sha256_uc"), NULL, ngx_http_upload_sha256_variable,
+ (uintptr_t) "0123456789ABCDEF",
+ NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
+
+ { ngx_string("upload_file_sha512"), NULL, ngx_http_upload_sha512_variable,
+ (uintptr_t) "0123456789abcdef",
+ NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
+
+ { ngx_string("upload_file_sha512_uc"), NULL, ngx_http_upload_sha512_variable,
+ (uintptr_t) "0123456789ABCDEF",
+ NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
+
{ ngx_string("upload_file_crc32"), NULL, ngx_http_upload_crc32_variable,
(uintptr_t) offsetof(ngx_http_upload_ctx_t, crc32),
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
@@ -762,6 +796,26 @@ ngx_http_upload_handler(ngx_http_request_t *r)
}else
u->sha1_ctx = NULL;
+ if(ulcf->sha256) {
+ if(u->sha256_ctx == NULL) {
+ u->sha256_ctx = ngx_palloc(r->pool, sizeof(ngx_http_upload_sha256_ctx_t));
+ if (u->sha256_ctx == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+ }
+ }else
+ u->sha256_ctx = NULL;
+
+ if(ulcf->sha512) {
+ if(u->sha512_ctx == NULL) {
+ u->sha512_ctx = ngx_palloc(r->pool, sizeof(ngx_http_upload_sha512_ctx_t));
+ if (u->sha512_ctx == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+ }
+ }else
+ u->sha512_ctx = NULL;
+
u->calculate_crc32 = ulcf->crc32;
u->request = r;
@@ -1215,6 +1269,12 @@ static ngx_int_t ngx_http_upload_start_handler(ngx_http_upload_ctx_t *u) { /* {{
if(u->sha1_ctx != NULL)
SHA1_Init(&u->sha1_ctx->sha1);
+ if(u->sha256_ctx != NULL)
+ SHA256_Init(&u->sha256_ctx->sha256);
+
+ if(u->sha512_ctx != NULL)
+ SHA512_Init(&u->sha512_ctx->sha512);
+
if(u->calculate_crc32)
ngx_crc32_init(u->crc32);
@@ -1308,6 +1368,12 @@ static void ngx_http_upload_finish_handler(ngx_http_upload_ctx_t *u) { /* {{{ */
if(u->sha1_ctx)
SHA1_Final(u->sha1_ctx->sha1_digest, &u->sha1_ctx->sha1);
+ if(u->sha256_ctx)
+ SHA256_Final(u->sha256_ctx->sha256_digest, &u->sha256_ctx->sha256);
+
+ if(u->sha512_ctx)
+ SHA512_Final(u->sha512_ctx->sha512_digest, &u->sha512_ctx->sha512);
+
if(u->calculate_crc32)
ngx_crc32_final(u->crc32);
@@ -1470,6 +1536,12 @@ static ngx_int_t ngx_http_upload_flush_output_buffer(ngx_http_upload_ctx_t *u, u
if(u->sha1_ctx)
SHA1_Update(&u->sha1_ctx->sha1, buf, len);
+ if(u->sha256_ctx)
+ SHA256_Update(&u->sha256_ctx->sha256, buf, len);
+
+ if(u->sha512_ctx)
+ SHA512_Update(&u->sha512_ctx->sha512, buf, len);
+
if(u->calculate_crc32)
ngx_crc32_update(&u->crc32, buf, len);
@@ -2020,6 +2092,14 @@ ngx_http_upload_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
conf->sha1 = prev->sha1;
}
+ if(prev->sha256) {
+ conf->sha256 = prev->sha256;
+ }
+
+ if(prev->sha512) {
+ conf->sha512 = prev->sha512;
+ }
+
if(prev->crc32) {
conf->crc32 = prev->crc32;
}
@@ -2178,6 +2258,80 @@ ngx_http_upload_sha1_variable(ngx_http_request_t *r,
return NGX_OK;
} /* }}} */
+static ngx_int_t /* {{{ ngx_http_upload_sha256_variable */
+ngx_http_upload_sha256_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ ngx_uint_t i;
+ ngx_http_upload_ctx_t *u;
+ u_char *c;
+ u_char *hex_table;
+
+ u = ngx_http_get_module_ctx(r, ngx_http_upload_module);
+
+ if(u->sha256_ctx == NULL || u->partial_content) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+
+ hex_table = (u_char*)data;
+ c = u->sha256_ctx->sha256_digest + SHA256_DIGEST_LENGTH * 2;
+
+ i = SHA256_DIGEST_LENGTH;
+
+ do{
+ i--;
+ *--c = hex_table[u->sha256_ctx->sha256_digest[i] & 0xf];
+ *--c = hex_table[u->sha256_ctx->sha256_digest[i] >> 4];
+ }while(i != 0);
+
+ v->data = u->sha256_ctx->sha256_digest;
+ v->len = SHA256_DIGEST_LENGTH * 2;
+
+ return NGX_OK;
+} /* }}} */
+
+static ngx_int_t /* {{{ ngx_http_upload_sha512_variable */
+ngx_http_upload_sha512_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ ngx_uint_t i;
+ ngx_http_upload_ctx_t *u;
+ u_char *c;
+ u_char *hex_table;
+
+ u = ngx_http_get_module_ctx(r, ngx_http_upload_module);
+
+ if(u->sha512_ctx == NULL || u->partial_content) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+
+ hex_table = (u_char*)data;
+ c = u->sha512_ctx->sha512_digest + SHA512_DIGEST_LENGTH * 2;
+
+ i = SHA512_DIGEST_LENGTH;
+
+ do{
+ i--;
+ *--c = hex_table[u->sha512_ctx->sha512_digest[i] & 0xf];
+ *--c = hex_table[u->sha512_ctx->sha512_digest[i] >> 4];
+ }while(i != 0);
+
+ v->data = u->sha512_ctx->sha512_digest;
+ v->len = SHA512_DIGEST_LENGTH * 2;
+
+ return NGX_OK;
+} /* }}} */
+
static ngx_int_t /* {{{ ngx_http_upload_crc32_variable */
ngx_http_upload_crc32_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
@@ -2411,6 +2565,10 @@ ngx_http_upload_set_form_field(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
", upload_file_md5_uc"
", upload_file_sha1"
", upload_file_sha1_uc"
+ ", upload_file_sha256"
+ ", upload_file_sha256_uc"
+ ", upload_file_sha512"
+ ", upload_file_sha512_uc"
", upload_file_crc32"
", upload_content_range"
" and upload_file_size"
@@ -2424,6 +2582,12 @@ ngx_http_upload_set_form_field(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
if(v->get_handler == ngx_http_upload_sha1_variable)
ulcf->sha1 = 1;
+ if(v->get_handler == ngx_http_upload_sha256_variable)
+ ulcf->sha256 = 1;
+
+ if(v->get_handler == ngx_http_upload_sha512_variable)
+ ulcf->sha512 = 1;
+
if(v->get_handler == ngx_http_upload_crc32_variable)
ulcf->crc32 = 1;
}

0 comments on commit 468725b

Please sign in to comment.
Something went wrong with that request. Please try again.