無限不可能性ドライブ

『ニューラルネットワーク自作入門』に刺激されてExcelVBAでニューラルネットワークを作ってみたものの、やっぱり数学やらなきゃと思い少しずつやってきたのもあって、自分の知識の整理とかそういった感じです。

【VBA】パスワードをハッシュ化する

f:id:celaeno42:20210312225434p:plain

Option Explicit

Public Sub ハッシュ値取得()
    Dim pw As String
    Dim hash As String
    Dim filePath As String
    Dim fso As Object
    
    Range("B1").Clear
    pw = Range("A1").Value
    If pw = "" Then
        Exit Sub
    End If
    
    'パスワードを書き込んだテキストファイルを生成
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    filePath = fso.GetSpecialFolder(2) & "\temp.txt"
    With fso.CreateTextFile(filePath)
        .Write pw
        .Close
    End With
    
    'ハッシュ値を取得
    hash = getHash(filePath)
    
    Range("B1").Value = hash
    
    'テキストファイルを削除
    On Error Resume Next
    fso.DeleteFile filePath
    
End Sub

Private Function getHash(ByRef filePath As String) As String
    Dim hashArray() As String
    Dim shell As Object
    Dim res As Object
    
    Set shell = CreateObject("WScript.Shell")
    
    Set res = shell.exec("%ComSpec% /c CertUtil -hashfile """ & filePath & """ SHA256")
'    Set res = shell.exec("%ComSpec% /c CertUtil -hashfile """ & filePath & """ MD5")
    
    Do While res.Status = 0
        DoEvents
    Loop
    
    hashArray = Split(res.StdOut.ReadAll, vbCrLf)
    
    getHash = Trim(hashArray(1))
    
End Function



【結果】
対象の文字列:
 password
SHA256でハッシュ化:
 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
MD5でハッシュ化:
 5f4dcc3b5aa765d61d8327deb882cf99