0

Can anyone tell me how to do this?

I have done hashing using sha1 in c#, but how to achieve the same hashing functionality using javascript?

I would like to learn this using different techniques. thanks!

Edit: I have tried following link: http://coursesweb.net/javascript/sha1-encrypt-data_cs

I didn't get the reason, why do this differs with result of SHA1CryptoServiceProvider of c# & example in mentioned link

Here is the code, That I have tried : 1. example mentioned in the above link(javascript) 2.

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

code behind:

    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();
}

}

2
  • I have tried SHA1CryptoServiceProvider while using c#, but I'm not aware how to make it work using javascript. Commented Feb 21, 2013 at 12:29
  • I have updated my try out now.. Commented Feb 21, 2013 at 13:04

3 Answers 3

2

Javascript does not have any built in hashing functions so you'll have to use an external library.

There is also another question on StackOverflow about this that has more information.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try this links to solve your problem:

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

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

Comments

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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.