Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ExcelでSHA256ăƒăƒƒă‚·ăƒ„é–ąæ•°ă‚’äœżç”šă™ă‚‹

Posted at

Excelă§ă€ăŸăŸăƒŒă«ăƒăƒƒă‚·ăƒ„é–ąæ•°ă‚’äœżç”šă—ăŸă„æ™‚ăŒă‚ă‚‹ă€‚
ăƒ‡ăƒ•ă‚©ăƒ«ăƒˆă§ăŻćŻŸćżœă—ăŠă„ăȘいぼで独è‡Șé–ąæ•°ă‚’ćźšçŸ©ă™ă‚‹ćż…èŠăŒă‚ă‚Šă€ă‚°ă‚°ă‚Œă°æƒ…ć ±ăŻăƒ’ăƒƒăƒˆă™ă‚‹ăźă ăŒă€ć°‘ă—ăƒăƒžăŁăŸăźă§èš˜éŒČしどおく。

環汃

Windows10
Office365 ćŸ·ç­†æ™‚ç‚čăƒăƒŒă‚žăƒ§ăƒł(ăƒăƒŒă‚žăƒ§ăƒł 2109 ăƒ“ăƒ«ăƒ‰ 16.0.14430.20314) 64 ビット

手順

1. ăƒžă‚Żăƒ­æœ‰ćŠčブック(*.xlsm)べしどExcelを保歘する。

2. [開ç™ș]タブから[Visual Basic]ă‚’éžæŠžă™ă‚‹ă€‚

3. èĄšç€șă•ă‚ŒăŸç”»éąć·Šăƒšă‚€ăƒłă§ćłă‚ŻăƒȘックし、[æŒżć…„]->[æš™æș–ăƒąă‚žăƒ„ăƒŒăƒ«]ă‚’éžæŠžă€‚

image.png

4. äœœæˆă•ă‚ŒăŸModule1ă«äž‹èš˜ă‚łăƒŒăƒ‰ă‚’èČŒă‚Šä»˜ă‘ă€‚

Public Function GetSHA256(ăƒăƒƒă‚·ăƒ„ć€€çź—ć‡ș範ć›Č As Range) As String

    Const LENGTH_OF_HASH = 32
    Const LENGTH_OF_HASH_AS_STRING = 64
    Set objUTF8 = CreateObject("System.Text.UTF8Encoding")
    Set objSHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")

    Dim str As String

    'Rangeăźæ–‡ć­—ćˆ—ç”ćˆïŒˆăƒŻăƒŒă‚Żă‚·ăƒŒăƒˆé–ąæ•°ăźConcată‚’æ”ç”šïŒ‰
    str = Application.WorksheetFunction.Concat(ăƒăƒƒă‚·ăƒ„ć€€çź—ć‡ș範ć›Č)

    'ăƒă‚€ăƒˆèȘ­ăżèŸŒăż
    Dim code() As Byte
    code = objUTF8.GetBytes_4(str)

    'ăƒăƒƒă‚·ăƒ„ć€€èšˆçź—
    Dim hashValue() As Byte
    hashValue = objSHA256.ComputeHash_2(code)

    '16é€Čæ•°ăžć€‰æ›
    Dim description As String * LENGTH_OF_HASH_AS_STRING
    Dim i&
    For i = 0 To LENGTH_OF_HASH - 1
        Mid(description, i * 2 + 1) = Right("0" & Hex(hashValue(i)), 2)
    Next i

    'return
    GetSHA256 = LCase(description)
    
End Function

ă‚łăƒŒăƒ‰ăŻäž‹èš˜ăƒȘンクでçŽčä»‹ă•ă‚ŒăŠă„ăŸă‚‚ăźă‚’ă»ăŒăăźăŸăŸćˆ©ç”šă•ă›ăŠé ‚ăă€‚

これで、GetSHA256é–ąæ•°ăŒä»•æ§˜ć‡șæ„ă‚‹ă‚ˆă†ă«ăȘă‚‹ăŻăšă ăŒé–ąæ•°ăŒć‹•ă‹ăȘい。
ここで民しハマった。

5. .Net Framework 3.5ăźæœ‰ćŠč挖

.Net Framework 3.5ăŒæœ‰ćŠčでăȘいべSystem.Security.Cryptography.SHA256ManagedがćŠčかăȘいらしい。
è‡Șćˆ†ăźç’°ćąƒă§ăŻăƒ‡ăƒ•ă‚©ăƒ«ăƒˆç„ĄćŠčçŠ¶æ…‹ă ăŁăŸăźă§ă€æœ‰ćŠč挖しどやる。

ă€Œă‚łăƒłăƒˆăƒ­ăƒŒăƒ«ăƒ‘ăƒăƒ«>ăƒ—ăƒ­ă‚°ăƒ©ăƒ >Windowsăźæ©Ÿèƒœăźæœ‰ćŠčćŒ–ăŸăŸăŻç„ĄćŠč挖」べé€Čみ、チェック。

image.png

6. ć‹•ăă‚ˆă†ă«ăȘった。

image.png

8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Today's trending articles

t0hara

[ă‚ąăƒŒă‚­ăƒ†ă‚ŻăƒăƒŁConference2025 ć‚ćŠ ăƒŹăƒăƒŒăƒˆ]ăƒȘă‚ąăƒ«ăȘçŸ„èŠ‹ă‹ă‚‰ć­Šă¶æœ€é©ăȘă‚ąăƒŒă‚­ăƒ†ă‚ŻăƒăƒŁ

Comments

No comments

Let's comment your feelings that are more than good

Qiita Advent Calendar is held!

Qiita Advent Calendar is an article posting event where you post articles by filling a calendar 🎅

Some calendars come with gifts and some gifts are drawn from all calendars 👀

Please tie the article to your calendar and let's enjoy Christmas together!

8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address