0

谁能告诉我该怎么做?

我已经在 c# 中使用 sha1 完成了散列,但是如何使用 javascript 实现相同的散列功能?

我想用不同的技术来学习这个。谢谢!

编辑:我试过以下链接: http ://coursesweb.net/javascript/sha1-encrypt-data_cs

我没有明白原因,为什么这与 c# 的 SHA1CryptoServiceProvider 的结果和提到的链接中的示例不同

这是我尝试过的代码:1.上面链接中提到的示例(javascript)2。

 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <asp:Button ID="Button1" runat="server" Text="Button_ServerSide" onclick="Button1_Click" />

后面的代码:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Security.Cryptography;
    using System.Text;

   public partial class Default2 : System.Web.UI.Page
   {
   protected void Page_Load(object sender, EventArgs e)
   {

   } 
   protected void Button1_Click(object sender, EventArgs e)
   {
    string strText = String.Empty;
    strText = TextBox1.Text;
    //SHA512CryptoServiceProvider encrypt = new SHA512CryptoServiceProvider();
    SHA1CryptoServiceProvider encrypt = new SHA1CryptoServiceProvider();
    byte[] encryptText = encrypt.ComputeHash(Encoding.Default.GetBytes(strText));

    TextBox1.Text = "";


    foreach (byte tempData in strText)
    {
        TextBox1.Text = TextBox1.Text + "x";

    }
    string str = System.Text.Encoding.Default.GetString(encryptText);
    Response.Write("Entered Text: " + strText + "  Encrypted Text Length: " + encryptText.Length + "    enpwd: " + strText);
    Response.Write("           encryptText: " + encryptText.Equals(strcrypt.Value));
    Response.Write("           encryptText STR: " + str);


    //ProtectedData.Protect();
}

}

4

3 回答 3

2

Javascript 没有任何内置的散列函数,因此您必须使用外部库。

StackOverflow 上还有另一个关于此的问题,其中包含更多信息。

于 2013-02-21T12:29:25.943 回答
1

你可以试试这个链接来解决你的问题:

http://caligatio.github.com/jsSHA/

http://www.movable-type.co.uk/scripts/sha1.html

于 2013-02-21T12:26:28.593 回答
0

sha1 = function(r) {
    var t = function() {
        function r() {
            e[0] = 1732584193, e[1] = 4023233417, e[2] = 2562383102, e[3] = 271733878, e[4] = 3285377520, 
            s = c = 0;
        }
        function t(r) {
            var t, n, o, f, a, u, c, s;
            for (t = i, n = 0; 64 > n; n += 4) t[n / 4] = r[n] << 24 | r[n + 1] << 16 | r[n + 2] << 8 | r[n + 3];
            for (n = 16; 80 > n; n++) r = t[n - 3] ^ t[n - 8] ^ t[n - 14] ^ t[n - 16], t[n] = 4294967295 & (r << 1 | r >>> 31);
            for (r = e[0], o = e[1], f = e[2], a = e[3], u = e[4], n = 0; 80 > n; n++) 40 > n ? 20 > n ? (c = a ^ o & (f ^ a), 
            s = 1518500249) : (c = o ^ f ^ a, s = 1859775393) : 60 > n ? (c = o & f | a & (o | f), 
            s = 2400959708) : (c = o ^ f ^ a, s = 3395469782), c = (4294967295 & (r << 5 | r >>> 27)) + c + u + s + t[n] & 4294967295, 
            u = a, a = f, f = 4294967295 & (o << 30 | o >>> 2), o = r, r = c;
            e[0] = e[0] + r & 4294967295, e[1] = e[1] + o & 4294967295, e[2] = e[2] + f & 4294967295, 
            e[3] = e[3] + a & 4294967295, e[4] = e[4] + u & 4294967295;
        }
        function n(r, n) {
            if ("string" == typeof r) {
                for (var o = [], e = 0, i = (r = unescape(encodeURIComponent(r))).length; e < i; ++e) o.push(r.charCodeAt(e));
                r = o;
            }
            if (n || (n = r.length), o = 0, 0 == c) for (;o + 64 < n; ) t(r.slice(o, o + 64)), 
            o += 64, s += 64;
            for (;o < n; ) if (f[c++] = r[o++], s++, 64 == c) for (c = 0, t(f); o + 64 < n; ) t(r.slice(o, o + 64)), 
            o += 64, s += 64;
        }
        function o() {
            var r, o, i = [], u = 8 * s;
            for (n(a, 56 > c ? 56 - c : 64 - (c - 56)), r = 63; 56 <= r; r--) f[r] = 255 & u, 
            u >>>= 8;
            for (t(f), r = u = 0; 5 > r; r++) for (o = 24; 0 <= o; o -= 8) i[u++] = e[r] >> o & 255;
            return i;
        }
        var e, f, i, a, u, c, s;
        for (e = [], f = [], i = [], a = [ 128 ], u = 1; 64 > u; ++u) a[u] = 0;
        return r(), {
            reset: r,
            update: n,
            digest: o,
            digestString: function() {
                for (var r = o(), t = "", n = 0; n < r.length; n++) t += "0123456789ABCDEF".charAt(Math.floor(r[n] / 16)) + "0123456789ABCDEF".charAt(r[n] % 16);
                return t;
            }
        };
    }();
    return t.update(r), t.digestString().toLowerCase();
};

console.log(sha1("admin"));

于 2021-09-08T05:10:51.143 回答