玩酷之家

 找回密码
 立即注册
查看: 1149|回复: 0

VB.NET 获取文件的效验值

[复制链接]

20

主题

21

帖子

909

积分

高级会员

Rank: 4

积分
909
QQ
发表于 2019-5-12 21:22:02 | 显示全部楼层 |阅读模式
本帖最后由 1059-Studio 于 2019-5-12 21:25 编辑

实测 NET 4.0 运行库正常运行
命名空间
  1. Imports System.Text
  2. Imports System.Security.Cryptography
  3. Imports System.IO
  4. Imports Microsoft.VisualBasic
复制代码
需要的自定义函数
  1.     Private Shared Function GetStringFromHash(hash As Byte()) As String
  2.         Dim result As New StringBuilder()
  3.         For i As Integer = 0 To hash.Length - 1
  4.             result.Append(hash(i).ToString("X2"))
  5.         Next
  6.         Return result.ToString()
  7.     End Function
复制代码
获取文件的MD5值
  1.     Public Shared Function getMD5(ByVal strSource As String) As String
  2.         Dim result As String = ""
  3.         Try
  4.             'strSource = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\path.txt"
  5.             Dim fstream As New FileStream(strSource, FileMode.Open, FileAccess.Read)
  6.             Dim dataToHash(fstream.Length - 1) As Byte
  7.             fstream.Read(dataToHash, 0, fstream.Length)
  8.             fstream.Close()
  9.             Dim hashvalue As Byte() = CType(CryptoConfig.CreateFromName("MD5"), HashAlgorithm).ComputeHash(dataToHash)
  10.             Dim i As Integer
  11.             For i = 0 To hashvalue.Length - 1
  12.                 result += Microsoft.VisualBasic.Right("00" + Hex(hashvalue(i)).ToLower, 2)
  13.             Next
  14.             Return result
  15.         Catch ex As Exception
  16.             'MsgBox(ex.Message)
  17.             Return result
  18.         End Try
  19.     End Function
复制代码
获取文件的SHA1值
  1.     Private Shared Function FileEncryptSHA1(ByVal file_name As String)
  2.         Dim sha1 As SHA1 = sha1.Create()
  3.         Dim hashValue() As Byte
  4.         Dim fStream As FileStream = File.OpenRead(file_name)
  5.         fStream.Position = 0
  6.         hashValue = sha1.ComputeHash(fStream)
  7.         Dim hash = GetStringFromHash(hashValue)
  8.         fStream.Close()
  9.         Return hash
  10.     End Function
复制代码
获取文件的SHA256值
  1.     Private Shared Function FileEncryptSHA256(ByVal file_name As String)
  2.         Dim sha256 As SHA256 = sha256.Create()
  3.         Dim hashValue() As Byte
  4.         Dim fStream As FileStream = File.OpenRead(file_name)
  5.         fStream.Position = 0
  6.         hashValue = sha256.ComputeHash(fStream)
  7.         Dim hash = GetStringFromHash(hashValue)
  8.         fStream.Close()
  9.         Return hash
  10.     End Function
复制代码
获取文件的SHA384值
  1.     Private Shared Function FileEncryptSHA384(ByVal file_name As String)
  2.         Dim sha384 As SHA384 = sha384.Create()
  3.         Dim hashValue() As Byte
  4.         Dim fStream As FileStream = File.OpenRead(file_name)
  5.         fStream.Position = 0
  6.         hashValue = sha384.ComputeHash(fStream)
  7.         Dim hash = GetStringFromHash(hashValue)
  8.         fStream.Close()
  9.         Return hash
  10.     End Function
复制代码
获取文件的SHA512值:
  1.     Private Shared Function FileEncryptSHA512(ByVal file_name As String)
  2.         Dim sha512 As SHA512 = sha512.Create()
  3.         Dim hashValue() As Byte
  4.         Dim fStream As FileStream = File.OpenRead(file_name)
  5.         fStream.Position = 0
  6.         hashValue = sha512.ComputeHash(fStream)
  7.         Dim hash = GetStringFromHash(hashValue)
  8.         fStream.Close()
  9.         Return hash
  10.     End Function
复制代码
湖边的稻草 1059 Studio
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|玩酷之家 ( 鄂ICP备14012049号-1 )|

GMT+8, 2020-8-4 17:15 , Processed in 1.076402 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表