Tiedostolle lasketulla tarkistussummalla voidaan valvoa että tiedostoon ei ole tehty luvattomia muutoksia. Nyt kuitenkin MD5 tarkistussumma näyttää uutisten mukaan olevan haavoittuva, joten useampi taho on jo siirtynyt SHA1 tarkistussumman käyttöön.
SHA1 algoritmi löytyy valmiina vb.net kirjastosta. Oheinen ohjelman pätkä näyttää miten sitä käytetään.
Projektissa on
button1
textbox1 : laskettu summa
textbox2 : oikeaksi tiedetty summa
label1: "Sha1Sum"
label2: "Oikeaksi tiedetty summa"
label3
aivan teksin alkuun tulee
Imports System.Security.Cryptography
Imports System.io
'
'..........................................
'
Function LaskeTiiviste(ByVal filename As String) As String
'******************************************************************
' SHA1 tarkistussumman (160 bit)laskenta tiedostolle
' palauttaa hex stringin, jossa väliviivat joka neljännen jälkeen
' by TNB 2005
'******************************************************************
'oliko tiedosto olemassa
If File.Exists(filename) = False Then Return "File not exists" 'palaa jos ei tiedostoa
'alustetaan
Dim fs As FileStream = New FileStream(filename, FileMode.Open, FileAccess.Read) 'tiedosto auki
Dim tiiviste1 As New SHA1Managed 'SHA1 luokkasta instanssi
Dim tiivisteData() As Byte ' tilaa tulokselle byteinä
'lasketaan SHA1 tiiviste
tiivisteData = tiiviste1.ComputeHash(fs)
'vapautetaan resurssit
fs.Close()
tiiviste1.Clear()
'muutetaan hex-tekstiksi, väliviiva
Dim i As Integer : Dim str As String
For i = 0 To UBound(tiivisteData)
If (i Mod 2 = 0) And i <> 0 Then str = str & "-"
If tiivisteData(i) < 16 Then
str = str & "0" & Hex(tiivisteData(i))
Else
str = str & Hex(tiivisteData(i))
End If
Next
Return str 'paluu
End FunctionKäyttöliittymä Button1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' AVAA TIEDOSTO
'tarkistettavan tiedoston nimi selbville
Dim openFileDialog1 As New OpenFileDialog
Dim st As String 'tila tiedostonimelle
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
TextBox1.Clear()
If openFileDialog1.ShowDialog() = DialogResult.OK Then
st = openFileDialog1.FileName
If File.Exists(st) Then
TextBox1.Text = LaskeTiiviste(st)
End If
End If
'vertailu oikeaksi tunnettuun arvoon
If TextBox1.Text = TextBox2.Text Then
Label3.Text = "OK"
Else
Label3.Text = "VIRHE"
End If
End Submuuttunut
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
' vertailutekstiä muutettu
If TextBox1.Text = TextBox2.Text Then
Label3.Text = "OK"
Else
Label3.Text = "VIRHE"
End If
End Subformin asetukset
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(16, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(104, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Avaa tiedosto"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(144, 32)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(368, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(144, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(104, 16)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Sha1Sum:"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(144, 80)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(368, 20)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = ""
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(144, 64)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(176, 16)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Vertailu summa (oikeaksi tiedetty)"
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(24, 72)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(96, 40)
Me.Label3.TabIndex = 5
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(536, 134)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Sha1Sum"
Me.ResumeLayout(False)
End Sub
#End RegionMitäs höpäjät, kyllä se on juuri SHA-1 josta niitä haavoittuvuuksia on löydetty: http://www.schneier.com/blog/archives/2005/02/
Tarvinneekohan aivan kaikkia laittaa eri listaukseen... Minulle md5 riittää vallan hyvin - ainakin toistaiseksi. Tämänhän olisi kyllä saanut kokeilemallakin selville.
Näyttää että SHA-1 algoritmi takaa vieläkin, että tarvitaan 2^69 tiedostoa, jotta löytyisi kaksi, joilla on sama tarkistussumma. Aiempi teoreettinen arvo oli 2^80 (siis neliöjuuri 2^160 ) täysin satunnaisella brutal force menetelmällä.
Ongelma koskee vain digitaalista allekirjoitusta, ei tiedoston muuttumattomuuden valvontaa ("tuliko ladattua täysin alkuperäisen mukainen tiedosto" - ongelma).
Joa jollakin taholla on tykit 2^69 dokumentin laskemiseksi ja tallentamisesksi niin sama taho kyllä pystyy väärentämään myös mustekynä-allekirjoituksen.
2^69 on toki paljon, mutta huomattavasti pienempi luku kuin 2^80. Näin ollen SHA-1 on "rikottu". En niinkään ottanut kantaa SHA-1:n vahvuuteen, vaan koodivinkin kuvaukseen. Tietääkseni MD5:sta ei ole suurempia haavoittuvuuksia löydetty, enkä ole kuullut, että SHA-1 olisi jotenkin viime aikoina saavuttanut erityisen suurta suosiota (verrattuna MD5:n).
Tässä otteita: http://userpages.umbc.edu/~mabzug1/cs/md5/md5.
NEWS: as of 2004, MD5 has a known collision weakness. .... Synopsis: MD5 is not completely useless, but is now problematic for certain uses.
Oma tulkintani: MD5 kelpaa edelleen tiedostojen muuttumattomuuden tarkistamiseen henkilökohtaisessa käytössä muuta ei digitaaliseen allekirjoitukseen, jos allekirjoituksina on tarkoitus tehdä "iso joukko" määrämuotoisia sanomia (joille voi etukäteen etsiä törmääviä sanomia ja sitten vain odotella että vastaan tule sanoma, jolle on jo löytänyt "edullisen" törmäävän sanoman; oikeussalissa olisi kaksi osapuolta, joilla olisi kummallakin sanoma, joka vastaa allekirjoitettua sanoman MD5-tiivistettä).
Aihe on jo aika vanha, joten et voi enää vastata siihen.