Using CAPICOM.HashedData from ASP

More info about HashedData object at MSDN Library.

Output

MD5
---
5256AB57510A551DEF4A898A8B7E12A3
5256AB57510A551DEF4A898A8B7E12A3 (Expected Hash)

SHA512
------
19B84F95FEE814E16CD6229CD472C6E8676D3F6E6D9153A8A76383676BEBDB4902664917EC4DF4355AAFEACC36B12435B5C372A31B78A826C2FDDB0C400C2E6B
19B84F95FEE814E16CD6229CD472C6E8676D3F6E6D9153A8A76383676BEBDB4902664917EC4DF4355AAFEACC36B12435B5C372A31B78A826C2FDDB0C400C2E6B (Expected Hash)

Sources

/CAPICOM/HashedData.asp

<%@ language="VBScript" %>
<!--#include virtual="/lib/unit-tests.asp"-->
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="/lib/unit-tests.css" rel="stylesheet" type="text/css" />
<h1>Using CAPICOM.HashedData from ASP</h1>
<p>
	More info about HashedData object at <a href="https://msdn.microsoft.com/en-us/library/aa382440(v=vs.85).aspx">MSDN Library</a>.
</p>
<h2>Output</h2>
<div class="code"><pre><%
 
' CAPICOM_HASH_ALGORITHM enumeration <https://msdn.microsoft.com/en-us/library/aa375694(v=vs.85).aspx>
const CAPICOM_HASH_ALGORITHM_SHA1    = 0
const CAPICOM_HASH_ALGORITHM_MD2     = 1
const CAPICOM_HASH_ALGORITHM_MD4     = 2
const CAPICOM_HASH_ALGORITHM_MD5     = 3
const CAPICOM_HASH_ALGORITHM_SHA_256 = 4
const CAPICOM_HASH_ALGORITHM_SHA_384 = 5
const CAPICOM_HASH_ALGORITHM_SHA_512 = 6
 
const INPUT           = "Hello World αβω"
const EXPECTED_MD5    = "5256AB57510A551DEF4A898A8B7E12A3"
const EXPECTED_SHA512 = "19B84F95FEE814E16CD6229CD472C6E8676D3F6E6D9153A8A76383676BEBDB4902664917EC4DF4355AAFEACC36B12435B5C372A31B78A826C2FDDB0C400C2E6B"
 
dim hd, util
 
set hd = Server.createObject("CAPICOM.HashedData")' <https://msdn.microsoft.com/en-us/library/aa382440(v=vs.85).aspx>
set util = Server.createObject("CAPICOM.Utilities")' <https://msdn.microsoft.com/en-us/library/aa388176.aspx>
'Base64Decode            | Decodes a string from base64.
'Base64Encode            | Encodes a string as base64.
'BinaryStringToByteArray | Converts a binary-packed string to an array of bytes.
'BinaryToHex             | Converts a binary-packed string to a hexadecimal string.
'ByteArrayToBinaryString | Converts an array of bytes to a binary-packed string.
'GetRandom               | Generates a secure random number.
'HexToBinary             | Converts a hexadecimal string to a binary-packed string.
'LocalTimeToUTCTime      | Converts the computer's local time to Coordinated Universal Time.
'UTCTimeToLocalTime      | Converts Coordinated Universal Time to the computer's local time.
 
Response.write "MD5"
Response.write vbNewline
Response.write "---"
Response.write vbNewline
 
hd.algorithm = CAPICOM_HASH_ALGORITHM_MD5
hd.hash util.ByteArrayToBinaryString( utf8_to_bytes( INPUT ) )
Response.write hd.value
Response.write vbNewline
 
Response.write EXPECTED_MD5
Response.write " (Expected Hash)"
Response.write vbNewline
 
Response.write vbNewline
 
Response.write "SHA512"
Response.write vbNewline
Response.write "------"
Response.write vbNewline
 
hd.algorithm = CAPICOM_HASH_ALGORITHM_SHA_512
hd.hash util.ByteArrayToBinaryString( utf8_to_bytes( INPUT ) )
Response.write hd.value
Response.write vbNewline
 
Response.write EXPECTED_SHA512
Response.write " (Expected Hash)"
Response.write vbNewline
 
set util = nothing
set hd = nothing
 
function utf8_to_bytes(byVal utf8)
	with Server.createObject("ADODB.Stream")
		.mode = adModeReadWrite
		.open()
 
		.type    = adTypeText
		.charset = "UTF-8"
		.writeText utf8
 
		.position = 0
		.type     = adTypeBinary
 
		.position = 3' skips BOM
		utf8_to_bytes = .read()
 
		.close()
	end with
end function
 
%></pre></div>
<hr />
<h2>Sources</h2>
<h3><%= Request.ServerVariables("SCRIPT_NAME") %></h3>
<div class="code"><%= geshify( loadTextFile( Server.mapPath( Request.ServerVariables("SCRIPT_NAME") ) ), "asp" ) %></div>