Option Explicit Private Const C_SHA256 As Long = 1 Private Const C_SHA384 As Long = 2 Private Const C_SHA512 As Long = 3 '-------------------------------------------------------------- ' SHA256算出関数 '-------------------------------------------------------------- Public Function GetSHA256(ハッシュ値算出範囲 As Range) As Variant Application.Volatile GetSHA256 = ComputeSHA(C_SHA256, ハッシュ値算出範囲) End Function '-------------------------------------------------------------- ' SHA384算出関数 '-------------------------------------------------------------- Public Function GetSHA384(ハッシュ値算出範囲 As Range) As Variant Application.Volatile GetSHA384 = ComputeSHA(C_SHA384, ハッシュ値算出範囲) End Function '-------------------------------------------------------------- ' SHA2512算出関数 '-------------------------------------------------------------- Public Function GetSHA512(ハッシュ値算出範囲 As Range) As Variant Application.Volatile GetSHA512 = ComputeSHA(C_SHA512, ハッシュ値算出範囲) End Function Private Function ComputeSHA(lngType As Long, r As Range) As Variant Dim host As New CLRHost Call host.Initialize(False) host.CLRLoadAssembly ("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") Dim str As String 'Rangeの文字列結合(ワークシート関数のConcatを流用) str = Application.WorksheetFunction.Concat(r) 'バイト読み込み Dim code() As Byte Dim objUTF8 As mscorlib.Object Set objUTF8 = host.ToObject(host.CLRCreateObject("System.Text.UTF8Encoding")) code = host.CLRInvokeMethod(objUTF8, "GetBytes", str) Set objUTF8 = Nothing 'ハッシュ値計算 Dim hashValue() As Byte Dim objSHA As mscorlib.Object Select Case lngType Case C_SHA256 Set objSHA = host.ToObject(host.CLRCreateObject("System.Security.Cryptography.SHA256Managed")) Case C_SHA384 Set objSHA = host.ToObject(host.CLRCreateObject("System.Security.Cryptography.SHA384Managed")) Case C_SHA512 Set objSHA = host.ToObject(host.CLRCreateObject("System.Security.Cryptography.SHA512Managed")) End Select hashValue = host.CLRInvokeMethod(objSHA, "ComputeHash", code) Set objSHA = Nothing '16進数へ変換 Dim description As String description = "" Dim i As Long For i = LBound(hashValue()) To UBound(hashValue()) description = description & Right("0" & Hex(hashValue(i)), 2) Next i 'return ComputeSHA = LCase(description) Set host = Nothing End Function