2010-11-29 6 views
0

私はテキストを暗号化するために、いくつかのCコードを持っている:OpenSSL暗号化をPerlに翻訳するには?

ngx_int_t 
ngx_http_encrypted_session_3des_mac_encrypt (ngx_pool_t * pool, 
              ngx_log_t * log, 
              const u_char * iv, size_t iv_len, 
              const u_char * key, 
              size_t key_len, 
              const u_char * in, size_t in_len, 
              ngx_uint_t expires, 
              u_char ** dst, size_t * dst_len) 
{ 
    EVP_CIPHER_CTX ctx; 
    const EVP_CIPHER *cipher; 
    u_char *p, *data; 
    int ret; 
    size_t block_size, buf_size, data_size; 
    int len; 
    uint64_t expires_time; 
    time_t now; 
    if (key_len != ngx_http_encrypted_session_key_length) { 
     return NGX_ERROR; 
    } 
    EVP_CIPHER_CTX_init (&ctx); 
    cipher = EVP_aes_256_cbc(); 
    block_size = EVP_CIPHER_block_size (cipher); 
    data_size = in_len + sizeof (expires_time); 
    buf_size = MD5_DIGEST_LENGTH  /* for the digest */ 
     + (data_size + block_size - 1) /* for EVP_EncryptUpdate */ 
     +block_size    /* for EVP_EncryptFinal */ 
     ; 
    p = ngx_palloc (pool, buf_size + data_size); 
    if (p == NULL) { 
     return NGX_ERROR; 
    } 
    *dst = p; 
    data = p + buf_size; 
    ngx_memcpy (data, in, in_len); 
    if (expires == 0) { 
     expires_time = 0; 
    } else { 
     now = time (NULL); 
     if (now == -1) { 
      goto evp_error; 
     } 
     expires_time = (uint64_t) now + (uint64_t) expires; 
    } 
    expires_time = ngx_http_encrypted_session_htonll (expires_time); 
    ngx_memcpy (data + in_len, (u_char *) & expires_time, 
       sizeof (expires_time)); 
    MD5 (data, data_size, p); 
    p += MD5_DIGEST_LENGTH; 
    ret = EVP_EncryptInit (&ctx, cipher, key, iv); 
    if (!ret) { 
     goto evp_error; 
    } 
/* encrypt the raw input data */ 
    ret = EVP_EncryptUpdate (&ctx, p, &len, data, data_size); 
    if (!ret) { 
     goto evp_error; 
    } 
    p += len; 
    ret = EVP_EncryptFinal (&ctx, p, &len); 
    if (!ret) { 
     return NGX_ERROR; 
    } 
/* XXX we should still explicitly release the ctx 
* or we'll leak memory here */ 
    EVP_CIPHER_CTX_cleanup (&ctx); 
    p += len; 
    *dst_len = p - *dst; 
    if (*dst_len > buf_size) { 
     ngx_log_error (NGX_LOG_ERR, log, 0, 
         "encrypted_session: 3des_mac_encrypt: buffer error"); 
     return NGX_ERROR; 
    } 
    return NGX_OK; 

    evp_error: 
    EVP_CIPHER_CTX_cleanup (&ctx); 
    return NGX_ERROR; 
} 

をしかし、私は、Perlにこのロジックを翻訳したいです。なぜこのコードでMD5ハッシングが使用されていますか?どのPerlモジュールを使うべきですか?

答えて

3

MD5はここでメッセージダイジェストとして使用されます。これは簡単なSSLのように見えるので、あなたは(そのようNet::SSLEayまたはIO::Socket::SSLなど)perlのためのSSLモジュールのいずれかを使用することができるはずです

0

あなたは

にDigest :: SHA1

ダイジェスト:: MD5

モジュールをご覧ください。
MD5はわずか128ビットなので安全性を高めるために両方のアルゴリズムを混在させていると思います。

関連する問題