Blog

  • Detecting mixed case strings in ASP Classic

    I needed to detect a mixed case string in classic ASP. I define a mixed case string as containing both upper and lower case characters, like AuxagGsrLpa. I could not find a free function on the web to make this decision, so I wrote one.

    <%
    	Function isMixedCase( str )
    		'detects mixed case strings using the english alphabet
    		'ignores spaces and other non-alphabetic characters
    		str = trim( str )
    		isMixed = false
    		lastCase = ""
    		for i=1 to len( str )
    			currentChar = mid( str, i, 1 )
    			if asc( currentChar ) >= 65 and asc( currentChar ) <= 90 then
    				if lastCase <> "" and lastCase <> "upper" then
    					isMixed = true
    					exit for
    				else
    					lastCase = "upper"
    				end if
    			else
    				if asc( currentChar ) >= 97 and asc( currentChar ) <= 122 then
    					'lower
    					if lastCase <> "" and lastCase <> "lower" then
    						isMixed = true
    						exit for
    					else
    						lastCase = "lower"
    					end if
    				else
    					lastCase = ""
    				end if
    			end if
    		next
    		if isMixed then
    			isMixedCase = true
    		else
    			isMixedCase = false
    		end if
    	End Function
    %>

    Here are a few test strings to demonstrate the functionality:


    <% response.write "Hello " & isMixedCase( "Hello " ) & " <br>" response.write "hello! " & isMixedCase( "hello!" ) & " <br>" response.write "HELLO " & isMixedCase( "HELLO " ) & " <br>" response.write "hello there " & isMixedCase( "hello there" ) & " <br>" response.write "hellothere " & isMixedCase( "hellothere" ) & " <br>" response.write "hellotherE " & isMixedCase( "hellotherE" ) & " <br>" %>

    Here is the resulting HTML output of the tests:

    Hello True
    hello! False
    HELLO False
    hello there False
    hellothere False
    hellotherE True

  • Matching MD5 hashes in ASP.NET and ASP Classic

    The goal of this page is to put two MD5 code samples on the same web page, one for ASP classic and one for ASP.NET. String formatting (like ASCII vs UTF-8) can trip up coding these two routines. These two code samples will produce the same MD5 hash output.

    Classic ASP/VBScript MD5

    The ASP code is simple once you have a copy of md5.asp from http://frez.co.uk. The only file in the ZIP that you need is md5.asp.


    <!--#include file="md5.asp"-->
    <% response.write md5("[email protected]") %>

    ASP.NET MD5

    The keys to getting the .net output to match are encoding.ascii and toString("x2").

    <%@ Import Namespace="System.Security.Cryptography" %>   
    <script language="VB" runat="server">
    
    	Public Sub Page_Load( sender As Object, e As EventArgs )
    
    		Dim strPlainText as String = "[email protected]"
      		
    		dim md5Hasher as MD5 = MD5.create( )
    		
    		dim result as byte( ) = md5Hasher.computeHash( encoding.ascii.getBytes( strPlainText))
    		
    		for each singleByte as byte in result
    			response.write ( singleByte.ToString("x2") )
    		next
    		
    	End Sub
    
    </script>