asp / vbscript sha1() and md5() function using .Net Framework

Function sha1(instr)
  Dim asc, enc, bytes, outstr, pos
  'Borrow some objects from .NET (supported from 1.1 onwards)   Set asc = Server.CreateObject("System.Text.UTF8Encoding")
  Set enc = Server.CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")

  'Convert the string to a byte array and hash it   bytes = asc.GetBytes_4(instr)
  bytes = enc.ComputeHash_2((bytes))

  outstr = ""

  'Convert the byte array to a hex string   For pos = 1 To Lenb(bytes)
    outstr = outstr & LCase(Right("0" & Hex(Ascb(Midb(bytes, pos, 1))), 2))
  Next
  sha1 = outstr
  Set enc = Nothing
  Set asc = Nothing
End Function



Function md5(instr)
  Dim asc, enc, bytes, outstr, pos
  'Borrow some objects from .NET (supported from 1.1 onwards)   Set asc = Server.CreateObject("System.Text.UTF8Encoding")
  Set enc = Server.CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")

  'Convert the string to a byte array and hash it   bytes = asc.GetBytes_4(instr)
  bytes = enc.ComputeHash_2((bytes))

  outstr = ""

  'Convert the byte array to a hex string   For pos = 1 To Lenb(bytes)
    outstr = outstr & LCase(Right("0" & Hex(Ascb(Midb(bytes, pos, 1))), 2))
  Next
  md5 = outstr
  Set enc = Nothing
  Set asc = Nothing
End Function

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s