@@ -1080,7 +1080,7 @@ End Function
10801080Public Function Base64Encode (Text As String ) As String
10811081#If Mac Then
10821082 Dim web_Command As String
1083- web_Command = "printf " & PrepareTextForShell (Text) & " | openssl base64"
1083+ web_Command = "printf " & PrepareTextForPrintf (Text) & " | openssl base64"
10841084 Base64Encode = ExecuteInShell(web_Command).Output
10851085#Else
10861086 Dim web_Bytes() As Byte
@@ -1699,6 +1699,43 @@ Public Function PrepareTextForShell(ByVal web_Text As String) As String
16991699 PrepareTextForShell = web_Text
17001700End Function
17011701
1702+ ''
1703+ ' Prepare text for using with printf command
1704+ ' - Wrap in "..."
1705+ ' - Replace ! with '!' (reserved in bash)
1706+ ' - Escape \, `, $, and "
1707+ ' - Replace % with %% (used as an argument marker in printf)
1708+ '
1709+ ' @internal
1710+ ' @method PrepareTextForPrintf
1711+ ' @param {String} Text
1712+ ' @return {String}
1713+ ''
1714+ Public Function PrepareTextForPrintf (ByVal web_Text As String ) As String
1715+ ' Escape special characters (except for !)
1716+ web_Text = VBA.Replace(web_Text, "\" , "\\" )
1717+ web_Text = VBA.Replace(web_Text, "`" , "\`" )
1718+ web_Text = VBA.Replace(web_Text, "$" , "\$" )
1719+ web_Text = VBA.Replace(web_Text, "%" , "%%" )
1720+ web_Text = VBA.Replace(web_Text, """" , "\""" )
1721+
1722+ ' Wrap in quotes
1723+ web_Text = """" & web_Text & """"
1724+
1725+ ' Escape !
1726+ web_Text = VBA.Replace(web_Text, "!" , """'!'""" )
1727+
1728+ ' Guard for ! at beginning or end (""'!'"..." or "..."'!'"" -> '!'"..." or "..."'!')
1729+ If VBA.Left$(web_Text, 3 ) = """""'" Then
1730+ web_Text = VBA.Right$(web_Text, VBA.Len(web_Text) - 2 )
1731+ End If
1732+ If VBA.Right$(web_Text, 3 ) = "'""""" Then
1733+ web_Text = VBA.Left$(web_Text, VBA.Len(web_Text) - 2 )
1734+ End If
1735+
1736+ PrepareTextForPrintf = web_Text
1737+ End Function
1738+
17021739' ============================================= '
17031740' 8. Cryptography
17041741' ============================================= '
@@ -1724,7 +1761,7 @@ End Function
17241761Public Function HMACSHA1 (Text As String , Secret As String , Optional Format As String = "Hex" ) As String
17251762#If Mac Then
17261763 Dim web_Command As String
1727- web_Command = "printf " & PrepareTextForShell (Text) & " | openssl dgst -sha1 -hmac " & PrepareTextForShell(Secret)
1764+ web_Command = "printf " & PrepareTextForPrintf (Text) & " | openssl dgst -sha1 -hmac " & PrepareTextForShell(Secret)
17281765
17291766 If Format = "Base64" Then
17301767 web_Command = web_Command & " -binary | openssl enc -base64"
@@ -1771,7 +1808,7 @@ End Function
17711808Public Function HMACSHA256 (Text As String , Secret As String , Optional Format As String = "Hex" ) As String
17721809#If Mac Then
17731810 Dim web_Command As String
1774- web_Command = "printf " & PrepareTextForShell (Text) & " | openssl dgst -sha256 -hmac " & PrepareTextForShell(Secret)
1811+ web_Command = "printf " & PrepareTextForPrintf (Text) & " | openssl dgst -sha256 -hmac " & PrepareTextForShell(Secret)
17751812
17761813 If Format = "Base64" Then
17771814 web_Command = web_Command & " -binary | openssl enc -base64"
@@ -1820,7 +1857,7 @@ End Function
18201857Public Function MD5 (Text As String , Optional Format As String = "Hex" ) As String
18211858#If Mac Then
18221859 Dim web_Command As String
1823- web_Command = "printf " & PrepareTextForShell (Text) & " | openssl dgst -md5"
1860+ web_Command = "printf " & PrepareTextForPrintf (Text) & " | openssl dgst -md5"
18241861
18251862 If Format = "Base64" Then
18261863 web_Command = web_Command & " -binary | openssl enc -base64"
0 commit comments