@@ -1080,7 +1080,7 @@ End Function
1080
1080
Public Function Base64Encode (Text As String ) As String
1081
1081
#If Mac Then
1082
1082
Dim web_Command As String
1083
- web_Command = "printf " & PrepareTextForShell (Text) & " | openssl base64"
1083
+ web_Command = "printf " & PrepareTextForPrintf (Text) & " | openssl base64"
1084
1084
Base64Encode = ExecuteInShell(web_Command).Output
1085
1085
#Else
1086
1086
Dim web_Bytes() As Byte
@@ -1699,6 +1699,43 @@ Public Function PrepareTextForShell(ByVal web_Text As String) As String
1699
1699
PrepareTextForShell = web_Text
1700
1700
End Function
1701
1701
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
+
1702
1739
' ============================================= '
1703
1740
' 8. Cryptography
1704
1741
' ============================================= '
@@ -1724,7 +1761,7 @@ End Function
1724
1761
Public Function HMACSHA1 (Text As String , Secret As String , Optional Format As String = "Hex" ) As String
1725
1762
#If Mac Then
1726
1763
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)
1728
1765
1729
1766
If Format = "Base64" Then
1730
1767
web_Command = web_Command & " -binary | openssl enc -base64"
@@ -1771,7 +1808,7 @@ End Function
1771
1808
Public Function HMACSHA256 (Text As String , Secret As String , Optional Format As String = "Hex" ) As String
1772
1809
#If Mac Then
1773
1810
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)
1775
1812
1776
1813
If Format = "Base64" Then
1777
1814
web_Command = web_Command & " -binary | openssl enc -base64"
@@ -1820,7 +1857,7 @@ End Function
1820
1857
Public Function MD5 (Text As String , Optional Format As String = "Hex" ) As String
1821
1858
#If Mac Then
1822
1859
Dim web_Command As String
1823
- web_Command = "printf " & PrepareTextForShell (Text) & " | openssl dgst -md5"
1860
+ web_Command = "printf " & PrepareTextForPrintf (Text) & " | openssl dgst -md5"
1824
1861
1825
1862
If Format = "Base64" Then
1826
1863
web_Command = web_Command & " -binary | openssl enc -base64"
0 commit comments