| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 磁盘文件编程 >> 实例分析正文
最新推荐 更多内容
VB.Net文件CRC32算法的实例
VB.Net文件CRC32算法的实例
作者:Iva    来源:乐博网收集     更新时间:2009-8-6

以下代码为乐博网lob.cn从C#转化为vb.net:

  • Imports System
  • Imports System.Security.Cryptography
  • Public Class Crc32
  •     Inherits HashAlgorithm
  •     Public Const DefaultPolynomial As UInt32 = &Hedb88320
  •     Public Const DefaultSeed As UInt32 = &Hffffffff
  •    
  •     Private hash As UInt32
  •     Private seed As UInt32
  •     Private table As UInt32()
  •     Private Shared defaultTable As UInt32()
  •    
  •     Public Sub New()
  •         table = InitializeTable(DefaultPolynomial)
  •         seed = DefaultSeed
  •         Initialize()
  •     End Sub
  •    
  •     Public Sub New(ByVal polynomial As UInt32, ByVal seed As UInt32)
  •         table = InitializeTable(polynomial)
  •         Me.seed = seed
  •         Initialize()
  •     End Sub
  •    
  •     Public Overloads Overrides Sub Initialize()
  •         hash = seed
  •     End Sub
  •    
  •     Protected Overloads Overrides Sub HashCore(ByVal buffer As Byte(), ByVal start As Integer, ByVal length As Integer)
  •         hash = CalculateHash(table, hash, buffer, start, length)
  •     End Sub
  •    
  •     Protected Overloads Overrides Function HashFinal() As Byte()
  •         Dim hashBuffer As Byte() = UInt32ToBigEndianBytes(Not hash)
  •         Me.HashValue = hashBuffer
  •         Return hashBuffer
  •     End Function
  •    
  •     Public Overloads Overrides ReadOnly Property HashSize() As Integer
  •         Get
  •             Return 32
  •         End Get
  •     End Property
  •    
  •     Public Shared Function Compute(ByVal buffer As Byte()) As UInt32
  •         Return Not CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length)
  •     End Function
  •    
  •     Public Shared Function Compute(ByVal seed As UInt32, ByVal buffer As Byte()) As UInt32
  •         Return Not CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length)
  •     End Function
  •    
  •     Public Shared Function Compute(ByVal polynomial As UInt32, ByVal seed As UInt32, ByVal buffer As Byte()) As UInt32
  •         Return Not CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length)
  •     End Function
  •    
  •     Private Shared Function InitializeTable(ByVal polynomial As UInt32) As UInt32()
  •         If polynomial = DefaultPolynomial AndAlso defaultTable IsNot Nothing Then
  •             Return defaultTable
  •         End If     '更多vb.net源码和实例,请关注lob.cn (乐 博 网)
  •        
  •         Dim createTable As UInt32() = New UInt32(255) {}
  •         For i As Integer = 0 To 255
  •             Dim entry As UInt32 = DirectCast(i, UInt32)
  •             For j As Integer = 0 To 7
  •                 If (entry And 1) = 1 Then
  •                     entry = (entry >> 1) Xor polynomial
  •                 Else
  •                     entry = entry >> 1
  •                 End If
  •             Next
  •             createTable(i) = entry
  •         Next
  •        
  •         If polynomial = DefaultPolynomial Then
  •             defaultTable = createTable
  •         End If
  •        
  •         Return createTable
  •     End Function
  •    
  •     Private Shared Function CalculateHash(ByVal table As UInt32(), ByVal seed As UInt32, ByVal buffer As Byte(), ByVal start As Integer, ByVal size As Integer) As UInt32
  •         Dim crc As UInt32 = seed
  •         For i As Integer = start To size - 1
  •                         crc = (crc >> 8) Xor table(buffer(i) Xor crc And &Hff)
  •         Next
  •         Return crc
  •     End Function
  •    
  •     Private Function UInt32ToBigEndianBytes(ByVal x As UInt32) As Byte()
  •         Return New Byte() {CByte(((x >> 24) And &Hff)), CByte(((x >> 16) And &Hff)), CByte(((x >> 8) And &Hff)), CByte((x And &Hff))}
  •     End Function
  • End Class

    乐博网提示 调用方式为:

  • Dim crc32 As New Crc32()
  • Dim hash As [String] = [String].Empty
  • Using fs As FileStream = File.Open("c:\myfile.txt", FileMode.Open)
  •     For Each b As Byte In crc32.ComputeHash(fs)
  •         hash += b.ToString("x2").ToLower()
  •     Next
  • End Using
  • Console.WriteLine("CRC-32 is {0}", hash)
  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    VB.Net获取随机密码的实例
    VB.Net用Salt哈希数据的实例
    用DPAPI加密和解密数据的实例
    储存加密的数据库连接字符串到注册表的实例
    Salt加密数据的.Net实例
    Rijndael加密解密的实例
    AES加密的实例
    Rijndael加密的实例
    SHA256 Hash加密的实例
    VB.Net对XML元素进行加密解密的实例
    VB.Net文件SHA1算法的实例
    Blowfish加密算法的实例
    Rijndael类的实例
    TripleDES加解密的实例
    VB.Net实现des加密算法的实例
    VB.Net实现RSA加密的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]