|
Since it is practically impossible to find two documents that would
have the same hash value, a document's hash, often referred to
as a "thumbprint", can be used
to uniquely identify this document. If you are to develop a document repository,
such as a database of image files, and want to prevent the same document
to appear in your database twice, you can store the document's thumbprint
together with the document itself. This way every time
a new document is to be added. you can easily check whether it already exists
in your database by computing the new file's hash value and looking for a match in your database.
Most database management systems are capable of storing arbitrary files
in database tables as blobs. MS Access
can stores blobs in fields of the type OLE Object. The
corresponding data type in SQL Server is called IMAGE.
AspUpload can save uploaded files in the database in just one line of code.
The following code snippet captures files uploaded with a browser and
saves them in the field Blob of the table Images:
<%
Set Upload = Server.CreateObject("Persits.Upload")
Upload.Save "c:\upload" ' temp location
For Each File in Upload.Files
File.ToDatabase "DSN=AspEncrypt;", _
"INSERT INTO IMAGES(BLOB) VALUES(?)"
Next
%>
|
The method File.ToDatabase accepts two parameters: an ODBC connection
string and an SQL INSERT or UPDATE statement where the actual
blob is denoted by a question mark (?).
The following ASP code (also found in the file Samples/File_ID/UploadAndID.asp of the installation)
captures files uploaded with a browser, computes their hash values, checks whether
the values are already in in the database and if not saves the files to the database.
The code uses the Images table of the AspEncrypt.mdb sample database shipped with the product.
<%
Set Upload = Server.CreateObject("Persits.Upload")
Upload.Save "c:\upload"
Set CM = Server.CreateObject("Persits.CryptoManager")
Set Context = CM.OpenContext("", True)
Set Hash = Context.CreateHash ' create SHA Hash object
' Save files in the database as blobs along with their thumbprints
For Each File in Upload.Files
Hash.Reset ' clear hash
Hash.AddFile File.Path ' compute file hash
HashValue = Hash.Value.Hex
' Check if thumbprint already exists in database
set rs = Server.CreateObject("adodb.recordset")
SQL = "select * from Images where Thumbprint = '" & HashValue & "'"
rs.Open SQL, "DSN=AspEncrypt;UID=;PWD=;"
If rs.EOF Then
' Build SQL INSERT statement
SQL = "insert into Images( Blob, Thumbprint) values(?, '" & HashValue & "')"
' Save file in the database
File.ToDatabase "DSN=AspEncrypt;UID=;PWD=;", SQL
Else
Response.Write "File " & File.Path & " is already in the database"
End If
Next
%>
|
|
|
|
|
|