大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> HOW TO: Compute and Compare Hash Values Using Visu

HOW TO: Compute and Compare Hash Values Using Visu

时间: 2021-07-31 作者:daque

how to: compute and compare hash values using visual basic .net--------------------------------------------------------------------------------the information in this article applies to:microsoft visual basic .net beta 2 --------------------------------------------------------------------------------this article discusses a beta release of a microsoft product. the information in this article is provided as-is and is subject to change without notice. no formal product support is available from microsoft for this beta product. for information about obtaining support for a beta release, please see the documentation included with the beta product files, or check the web location from which you downloaded the release. for a microsoft c# .net version of this article, see q307020 . in this task summary requirements compute a hash value compare two hash values complete code listing references summarythe system.security.cryptography classes in the microsoft .net framework make it easy to compute a hash value for your source data. this article shows how to obtain a hash value and how to compare two hash values to check whether they are identical. back to the top requirements the following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need: microsoft visual studio .net back to the top compute a hash value it is easy to generate and compare hash values using the cryptographic resources contained in the system.security.cryptography namespace. because all hash functions take input of type byte[] , it might be necessary to convert the source into a byte array before it is hashed. to create a hash for a string value, follow these steps: open visual studio .net. create a new console application in visual basic .net. visual studio .net creates a module for you along with an empty main() procedure. make sure that the project references the system and system.security namespaces. use the imports statement on the system , system.security , system.security.cryptographic , and system.text namespaces so that you are not required to qualify declarations from these namespaces later in your code. these statements must be used prior to any other declarations. imports systemimports system.securityimports system.security.cryptographyimports system.text declare a string variable to hold your source data, and two byte arrays (of undefined size) to hold the source bytes and the resulting hash value. dim ssourcedata as stringdim tmpsource() as bytedim tmphash() as byte use the getbytes() function, which is part of the system.text.asciiencoding.ascii class, to convert your source string into an array of bytes (required as input to the hashing function). ssourcedata = "mysourcedata"'create a byte array from source data.tmpsource = asciiencoding.ascii.getbytes(ssourcedata) compute the md5 hash for your source data by calling computehash on an instance of the md5cryptoserviceprovider class. note that to compute another hash value, you will need to create another instance of the class. 'compute hash based on source data.tmphash = new md5cryptoserviceprovider().computehash(tmpsource) the tmphash byte array now holds the computed hash value (128-bit value=16 bytes) for your source data. it is often useful to display or store a value like this as a hexadecimal string, which the following code accomplishes: console.writeline(bytearraytostring(tmphash))private function bytearraytostring(byval arrinput() as byte) as stringdim i as integerdim soutput as new stringbuilder(arrinput.length)for i = 0 to arrinput.length - 1soutput.append(arrinput(i).tostring("x2"))nextreturn soutput.tostring()end function save and then run your code to see the resulting hexadecimal string for the source value. back to the top compare two hash values one of the purposes of creating a hash from source data is to provide a way to see if data has changed over time, or to compare two values without ever working with the actual values. in either case, you need to compare two computed hashes, which is easy if they are both stored as hexadecimal strings (as in the last step of the above section). however, it is quite possible that they will both be in the form of byte arrays. the following code, which continues from the code created in the previous section, shows how to compare two byte arrays. just below the creation of a hexadecimal string, create a new hash value based on new source data. ssourcedata = "notmysourcedata"tmpsource = asciiencoding.ascii.getbytes(ssourcedata)dim tmpnewhash() as bytedim bequal as boolean = falsetmpnewhash = new md5cryptoserviceprovider().computehash(tmpsource) the most straightforward way to compare two byte arrays is to loop through the arrays, comparing each individual element to its counterpart from the second value. if any elements are different, or if the two arrays are not the same size, the two values are not equal. if tmpnewhash.length = tmphash.length thendim i as integerdo while (i < tmpnewhash.length) andalso (tmpnewhash(i) = tmphash(i))i += 1loopif i = tmpnewhash.length thenbequal = trueend ifend ifif bequal thenconsole.writeline("the two hash values are the same")elseconsole.writeline("the two hash values are not the same")end ifconsole.readline() save and then run your project to view the hexadecimal string created from the first hash value, and to find out if the new hash is equal to the original. back to the top complete code listing imports systemimports system.securityimports system.security.cryptographyimports system.textmodule module1sub main()dim ssourcedata as stringdim tmpsource() as bytedim tmphash() as bytessourcedata = "mysourcedata"'create a byte array from source data.tmpsource = asciiencoding.ascii.getbytes(ssourcedata)'compute hash based on source data.tmphash = new md5cryptoserviceprovider().computehash(tmpsource)console.writeline(bytearraytostring(tmphash))ssourcedata = "notmysourcedata"tmpsource = asciiencoding.ascii.getbytes(ssourcedata)dim tmpnewhash() as bytedim bequal as boolean = falsetmpnewhash = new md5cryptoserviceprovider().computehash(tmpsource)if tmpnewhash.length = tmphash.length thendim i as integerdo while (i < tmpnewhash.length) andalso (tmpnewhash(i) = tmphash(i))i += 1loopif i = tmpnewhash.length thenbequal = trueend ifend ifif bequal thenconsole.writeline("the two hash values are the same")elseconsole.writeline("the two hash values are not the same")end ifconsole.readline()end subprivate function bytearraytostring(byval arrinput() as byte) as stringdim i as integerdim soutput as new stringbuilder(arrinput.length)for i = 0 to arrinput.length - 1soutput.append(arrinput(i).tostring("x2"))nextreturn soutput.tostring()end functionend module back to the top references for more information on using the cryptographic features of the microsoft .net framework, and on cryptography in general, see the following links: "the md5 message-digest algorithm" http://theory.lcs.mit.edu/~cis/pubs/rivest/rivest-md5.txt "what is a hashing function?" http://www.rsasecurity.com/rsalabs/faq/2-1-6.html msdn online .net developer web site http://msdn.microsoft.com/net back to the top &

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.