SHA hash + ToBase64String

ASP Classic, Code for SHA hash using mscorlib.dll

 
 

<%
dim concat_str

concat_str = "1030204645TT3343PipisCrew Order3.10EURDEMO@PIPISCREW.COM1http://localhost/success.htmlhttp://localhost:1400/cancel.html?orderId=13564MediaLink"

'Borrow some objects from .NET
Set ccc = CreateObject("System.Text.UTF8Encoding")
Set enc = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider") 

'Convert the string to a byte array and hash it
bytes = ccc.GetBytes_4(concat_str)
bytes = enc.ComputeHash_2((bytes)) 
'Convert the byte array to a hex string

'1st way
'zRRNmrTgh1lwz5HHj8HoGZ6wjms=
digest = encodeBase64(bytes)

'2nd way
'zRRNmrTgh1lwz5HHj8HoGZ6wjms=
digest = ToBase64String(bytes)

'notice - the ToBase64String/encodeBase64 output is the same with this piece of PHP code
'<?php
'$digest = base64_encode(sha1("1030204645TT3343PipisCrew Order3.10EURDEMO@PIPISCREW.COM1http://localhost/success.htmlhttp://localhost:1400/cancel.html?orderId=13564MediaLink",true));
'
'echo $digest;
'?>

Response.Write(digest)

Function ToBase64String(rabyt)
  
  'Ref: http://stackoverflow.com/questions/1118947/converting-binary-file-to-base64-string
  Dim xml: Set xml = CreateObject("MSXML2.DOMDocument.3.0")
  xml.LoadXml "<root/>"
  xml.documentElement.dataType = "bin.base64"
  xml.documentElement.nodeTypedValue = rabyt
  ToBase64String = Replace(xml.documentElement.Text,VbLf, "")
  
End Function

Function encodeBase64(bytes)
	dim DM, EL
	Set DM = CreateObject("Microsoft.XMLDOM")
	' Create temporary node with Base64 data type
	Set EL = DM.createElement("tmp")
	EL.DataType = "bin.base64"
	' Set bytes, get encoded String
	EL.NodeTypedValue = bytes
	encodeBase64 = EL.Text
End Function
  

%>

 
 

VBA Code for SHA-512 hash using mscorlib.dll

 
 

'source - http://amadiere.com/blog/category/programming/classic-asp-programming/

Function Hash(strPassword, strIndividualSalt)
 
  Const strSiteWideSalt = "THIS IS A SITE WIDE SALT, BUT COULD BE A GUID"
  Hash = HashSHA512Managed(strSiteWideSalt & strPassword & strIndividualSalt)
 
End Function
 
Function HashSHA512Managed(saltedPassword)
 
  Dim objMD5, objUTF8
  Dim arrByte
  Dim strHash
  Set objUnicode = CreateObject("System.Text.UnicodeEncoding")
  Set objSHA512 = Server.CreateObject("System.Security.Cryptography.SHA512Managed")
 
  arrByte = objUnicode.GetBytes_4(saltedPassword)
  strHash = objSHA512.ComputeHash_2((arrByte))
 
  HashSHA512Managed = ToBase64String(strHash)
 
End Function
 
Function ToBase64String(rabyt)
  'tested&working
  'Ref: http://stackoverflow.com/questions/1118947/converting-binary-file-to-base64-string
  Dim xml: Set xml = CreateObject("MSXML2.DOMDocument.3.0")
  xml.LoadXml "<root />"
  xml.documentElement.dataType = "bin.base64"
  xml.documentElement.nodeTypedValue = rabyt
  ToBase64String = Replace(xml.documentElement.Text,VbLf, "")
 
End Function

 
 

'source https://gist.github.com/jermity/b81622a2b10449c6be99

64-bit MS Access VBA code to calculate an SHA-512 or SHA-256 hash in VBA.  This requires a VBA reference to the .Net Framework 4.0 mscorlib.dll.  The hashed strings are calculated using calls to encryption methods built into mscorlib.dll.  The calculated hash strings are the same values as those calculated with jsSHA, a Javascript SHA implementation (see https://caligatio.github.io/jsSHA/ for an online calculator and the jsSHA code).

The mscorlib.dll is intended for .Net Framework managed applications, but the stackoverflow.com post showed how it could be used with MS Access VBA.  This technique is not documented anywhere in MS Access documentation that I could find, so the stackoverflow.com post was very helpful in this regard.  

Public Sub to_SHA512()
'Requires a reference to mscorlib 4.0 64-bit
Dim text As Object
Dim SHA512 As Object

Set text = CreateObject("System.Text.UTF8Encoding")
Set SHA512 = CreateObject("System.Security.Cryptography.SHA512Managed")
Set SHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")
'Debug.Print ToBase64String(SHA512.ComputeHash_2((text.GetBytes_4("mypassword"))))
Debug.Print ToHexString(SHA512.ComputeHash_2((text.GetBytes_4("mypassword"))))
End Sub

Public Sub to_SHA256()
'Requires a reference to mscorlib 4.0 64-bit, which is part of the .Net Framework 4.0
Dim text As Object
Dim SHA256 As Object

Set text = CreateObject("System.Text.UTF8Encoding")
Set SHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")
debug.print ToHexString(SHA256.ComputeHash_2((text.GetBytes_4("mypassword")))) 
End Sub

Function ToBase64String(rabyt)

  'Ref: http://stackoverflow.com/questions/1118947/converting-binary-file-to-base64-string
  With CreateObject("MSXML2.DOMDocument")
    .LoadXML "<root />"
    .DocumentElement.DataType = "bin.base64"
    .DocumentElement.nodeTypedValue = rabyt
    ToBase64String = Replace(.DocumentElement.text, vbLf, "")
  End With
End Function

Function ToHexString(rabyt)

  'Ref: http://stackoverflow.com/questions/1118947/converting-binary-file-to-base64-string
  With CreateObject("MSXML2.DOMDocument")
    .LoadXML "<root />"
    .DocumentElement.DataType = "bin.Hex"
    .DocumentElement.nodeTypedValue = rabyt
    ToHexString = Replace(.DocumentElement.text, vbLf, "")
  End With
End Function

 
 

VBScript read/write binary, encode/decode Base64

'https://ghads.wordpress.com/2008/10/17/vbscript-readwrite-binary-encodedecode-base64/
'
' Call via cmd: cscript ReadFileEncodeBase64DecodeBase64WriteFile.vbs [pathToFile]
'
' - Reads file from arg into byte array
' - Encodes byte array to Base64 String
' - Decodes Base64 String to byte array
' - Writes byte array to new file
'
' Sample WITHOUT any warrenty! Use at own risk! Copyright 2008 Gerhard Balthasar
'
  
Option Explicit
' common consts
Const TypeBinary = 1
Const ForReading = 1, ForWriting = 2, ForAppending = 8
  
' getting file from args (no checks!)
Dim arguments, inFile, outFile
  
Set arguments = WScript.Arguments
inFile = arguments(0)
outFile = "new_" & inFile
  
Dim inByteArray, base64Encoded, base64Decoded, outByteArray
 
inByteArray = readBytes(inFile)
base64Encoded = encodeBase64(inByteArray)
  
Wscript.echo "Base64 encoded: " + base64Encoded
  
base64Decoded = decodeBase64(base64Encoded)
writeBytes outFile, base64Decoded
  
Wscript.echo "Finished!"
  
private function readBytes(file)
  dim inStream
  ' ADODB stream object used
  set inStream = WScript.CreateObject("ADODB.Stream")
  ' open with no arguments makes the stream an empty container 
  inStream.Open
  inStream.type= TypeBinary
  inStream.LoadFromFile(file)
  readBytes = inStream.Read()
end function
  
private function encodeBase64(bytes)
  dim DM, EL
  Set DM = CreateObject("Microsoft.XMLDOM")
  ' Create temporary node with Base64 data type
  Set EL = DM.createElement("tmp")
  EL.DataType = "bin.base64"
  ' Set bytes, get encoded String
  EL.NodeTypedValue = bytes
  encodeBase64 = EL.Text
end function
  
private function decodeBase64(base64)
  dim DM, EL
  Set DM = CreateObject("Microsoft.XMLDOM")
  ' Create temporary node with Base64 data type
  Set EL = DM.createElement("tmp")
  EL.DataType = "bin.base64"
  ' Set encoded String, get bytes
  EL.Text = base64
  decodeBase64 = EL.NodeTypedValue
end function
  
private Sub writeBytes(file, bytes)
  Dim binaryStream
  Set binaryStream = CreateObject("ADODB.Stream")
  binaryStream.Type = TypeBinary
  'Open the stream and write binary data
  binaryStream.Open
  binaryStream.Write bytes
  'Save binary data to disk
  binaryStream.SaveToFile file, ForWriting
End Sub

 

Replicating PHP’s sha1() in VBScript

https://angrytechnician.wordpress.com/2011/02/11/replicating-phps-sha1-in-vbscript/
 

Option Explicit
 
Dim asc, enc, bytes, instr, outstr, pos
 
instr = "test string"
 
'Borrow some objects from .NET (supported from 1.1 onwards)
Set asc = CreateObject("System.Text.UTF8Encoding")
Set enc = 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
 
WScript.Echo outstr

By: |03/11/2015|categories: / /