【问题标题】:Is my understanding of this code correct/complete? [closed]我对这段代码的理解是否正确/完整? [关闭]
【发布时间】:2019-12-30 13:06:31
【问题描述】:

我正在维护一个客户 Classic ASP 网站,在特定位置发现了一些 ASP.NET 代码。我需要有人帮助我理解每一行的含义,因为我必须用经典的 ASP 函数替换这段 ASP.NET 代码。

据我了解,代码执行如下:

  1. 获取Request.QueryString“digest”,并将其放入名为“str”的变量中
  2. 将 Response.ContentType 设置为“text/plain”
  3. 使用“str”变量调用“HashCode”函数。此“HashCode”函数执行以下操作:
  4. 创建一个名为“hash”的 SHA1 哈希引擎实例
  5. 创建一个名为“encoder”的 UTF8 字符串编码器实例
  6. 计算变量“combined”,它必须是从“str”参数派生的字节序列
  7. 获取“组合”的 SHA1 哈希
  8. 返回先前计算的“组合”SHA1 哈希的 Base64 编码值
  9. Response.Write 这个返回值。

我想确保我没有遗漏任何其他内容。我的理解是否全面完整?

<%@ WebHandler Language="C#" Class="digets" %>

using System;
using System.Web;

public class digets : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        string str= context.Request.QueryString.Get("digest");

        context.Response.ContentType = "text/plain";
        context.Response.Write(HashCode(str));
    }

    public static string HashCode(string str)
    {
        string rethash = "";
        try
        {

            System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            byte[] combined = encoder.GetBytes(str);
            hash.ComputeHash(combined);
            rethash = Convert.ToBase64String(hash.Hash);
        }
        catch (Exception ex)
        {
            string strerr = "Error in HashCode : " + ex.Message;
        }
        return rethash;
    } 

    public bool IsReusable {
        get {
            return false;
        }
    }

}

【问题讨论】:

    标签: c# asp.net .net iis


    【解决方案1】:

    是的,你的理解是正确的。 它将使用 HashCode 方法生成哈希,然后将其写入响应。

    【讨论】:

      猜你喜欢
      • 2010-11-07
      • 2020-03-03
      • 2012-10-14
      • 2013-06-15
      • 2021-12-02
      • 2018-04-26
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多