Está en la página 1de 15

I.

1.

Hm bm MD5
M t thut ton MD5
Gi s chng ta c thng ip b bit u vo, v ta mun tm m s ca thng ip.
y b l s khng m bt k; b c th bng 0 v khng cn chia ht cho 8, ln c th
bt k. Tng tng rng cc bit ca thng ip c vit nh sau :
m_0 m_1 m_2 ... m_{b-1}
M s thng ip c tnh qua 5 bc sau
3.1 - Bc 1 : Cc bit gn thm
Thng ip c m rng, thm bit vo pha sau sao cho di ca n ( tnh theo bit )
ng d vi 448 theo mun 512. Ngha l thng ip c m rng sao cho n cn
thiu 64 bit na th s c mt di chia ht cho 512. Vic ny lung c thc hin
ngay c khi bn thn di thng ip ng d vi 448 theo mun 512.
Vic thm bit ny thc hin nh sau : mt bit ``1`` c thm vo sau thng ip, sau
cc bit ``0`` c thm vo c mt di ng d vi 448 mun 512. Trong tt
c cc trng hp, c t nht 1 v nhiu nht 512 bit c thm vo.
3.2 - Bc 2 : Gn thm di
Dng biu din 64 bit di b ca chui ban u c thm vo pha sau kt qu ca
bc 1. Trong trng hp b ln hn 2^64 th ch c 64 bit thp ca b c s dng.
( Cc bit ny c thm vo pha sau di dng 2 word 32 bit, gn word thp trc
theo quy c trn )
3.3 - Bc 3 : Khi to b m MD
Mt b m 4 word (A,B,C,D) c dng tnh m s thng ip. y mi A,B,C,D
l mt thanh ghi 32 bit. Nhng thanh ghi ny c khi to theo nhng gi tr hex sau
( cc byte thp trc ) :
word A : 01 23 45 67
word B : 89 ab cd ef
word C : fe dc ba 98
word D : 76 54 32 10
3.4 - Bc 4 : X l thng ip theo tng khi 16 word
Trc ht ta nh ngha cc hm ph, cc hm ny nhn u vo l 3 word 32 bit v to
ra mt word 32 bit.
F(X,Y,Z) = XY v not(X) Z
G(X,Y,Z)= XZ v Y not(Z)
H(X,Y,Z) = X xor Y xor Z
I(X,Y,Z) = Y xor (X v not(Z))

Vi mi bit, F hot ng nh mt iu kin : nu X th Y nu khng th Z. Hm F c th


nh ngha bng php + thay v v bi v XY v not(X)Z khng bao gi c ``1`` cng 1 v
tr bit.Cc hm G,H v I tng t nh F, ch chng tc ng theo tng bit tng ng
to ra kt qu t cc bit ca X,Y v Z
Bc ny s dng mt bng 64 gi tr T[1 .. 64] c to ra t hm sin. Gi T[i] l phn
t th i ca bng, th T[i]l phn nguyn ca 4294967296*|sin(i)| , i c tnh theo
radian.
Lm nh sau :
/* X l mi khi 16 word */
For i = 0 to N/16-1 do
/* Copy block i into X. */
For j = 0 to 15 do
Set X[j] to M[i*16+j].
end /* of loop on j */
/* Lu A vo AA, B vo BB, C vo CC, D v DD . */
AA = A
BB = B
CC = C
DD = D
/* Vng 1. */
/* K hiu [abcd k s i] ngha l thc hin nh sau :
a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
/* Thc hin : */
[ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4]
[ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8]
[ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12]
[ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16]
/* Vng 2. */
/*K hiu [abcd k s i] ngha l thc hin nh sau
a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
/* Thc hin : */
[ABCD 1 5 17] [DABC 6 9 18] [CDAB 11 14 19] [BCDA 0 20 20]
[ABCD 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24]
[ABCD 9 5 25] [DABC 14 9 26] [CDAB 3 14 27] [BCDA 8 20 28]
[ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32]
/* Vng 3. */

/* K hiu [abcd k s t] ngh l lm nh sau


a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
/* Thc hin :*/
[ABCD 5 4 33] [DABC 8 11 34] [CDAB 11 16 35] [BCDA 14 23 36]
[ABCD 1 4 37] [DABC 4 11 38] [CDAB 7 16 39] [BCDA 10 23 40]
[ABCD 13 4 41] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44]
[ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48]
/* Vng 4. */
/* K hiu [abcd k s t] ngha l lm nh sau
a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
/* Thc hin: */
[ABCD 0 6 49] [DABC 7 10 50] [CDAB 14 15 51] [BCDA 5 21 52]
[ABCD 12 6 53] [DABC 3 10 54] [CDAB 10 15 55] [BCDA 1 21 56]
[ABCD 8 6 57] [DABC 15 10 58] [CDAB 6 15 59] [BCDA 13 21 60]
[ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] [BCDA 9 21 64]
/* Then perform the following additions. (That is increment each
of the four registers by the value it had before this block was started.) */
/* Sau lm cc php cng sau. ( Ngha l cng vo mi thanh ghi gi tr ca n trc
khi vo vng lp ) */
A = A + AA
B = B + BB
C = C + CC
D = D + DD
end /* of loop on i */
3.5 - Bc 5 : In ra
M s thng ip c to ra l A,B,C,D. Ngha l chng ta bt u t byte thp ca A,
kt thc vi byte cao ca D.
2. c im MD5

Vic tnh MD5 n gin,c kh nng xc nh c file c kch thc nhiu Gb

Khng c kh nng tnh ngc khi tm ra MD

Do bn cht ca hm bm v s lng cc ln cc gi tr hash c th nn hu nh


khng th c 2 bn tin phn bit c cung gi tr Hash

Gi tr MD ph thuc vo bn tin tng ng

Mt chui c duy nht mt hash

Gi tr MD ph thuc vo tt c cc bits tng ng

3. Thi gian ph m

a. V MD5 ch d qua d liu mt ln, nu hai tin t vi cng bng bm c xy


nn, th cng mt hu t c th cng c thm vo khin cho ng d
xy ra. Tc l hai d liu vo (input) X v Y hon ton khc nhau nhng c th
ra (output) c mt md5 hash ging nhau . Tuy nhin xc sut xy ra iu
ny l kh nh.
b. V nhng k thut tm ng hin nay cho php cc trng thi bm trc
c xc nh mt cch ngu nhin, c th tm thy xung t i vi bt k tin
t mong mun no; c ngha l, i bt k mt chui cc k t X cho trc, hai
tp tin ng c th c xc nh m u bt u vi X.
c. Tt c nhng g cn to ra hai tp tin ng l mt tp tin mu, vi mt khi
d liu 128 byte c xp trn gii hn 64 byte, c th thay i t do bng gii
thut tm va chm.

d. Nh trn ni: t 1 hashes ta tm ngc li string ca n c khng? iu


ny khng bao gi lm c.
Vy y cu tr li l : Bn khng th . Cch hash mt string c ngha l tn
hiu md xut ra bng thut tan MD5 l khng th o ngc. Khng th bit
cch tm ra chui origin t tn hiu MD5 hashes. Tc l chng ta khng th no
decode (dch ngc) c mt chui MD5 hashes. Ch c mt cch c c
chui origin l bng cch brute force cracking. Tc l phi duyt qua rt nhiu
t hp cc k t ca chui vo cho n khi mt trong nhng chui md c to
ra ca chng bng vi chui md cn tm origin. Tuy nhin , vi kh nng my
tnh thi nay mun lm iu ny phi mt rt nhiu nm. Mt khc y chng
ta cn ch l tn hiu MD5 hashes c thit k ra l c nht v nh.
Trn l thuyt cng c kh nng 2 chui khc nhau c tn hiu md ging nhau
nhng kh nng ny rt rt nh (1/(16^32) hay vo khong (3.4E+38), v th
brute force mt md5 th my tnh chc chy vi tun cng cha tm ra.
4. Phn tch thi gian thc hin
5. Cc m hnh p dng trong lu tr, truyn thong c th p dng

a. 1/ MD5 c s dng rng ri trong th gii phn mm m bo rng tp tin


ti v khng b hng. Ngi s dng c th so snh gia thng s kim tra phn
mm bng MD5 c cng b vi thng s kim tra phn mm ti v bng
MD5. H iu hnh Unix s dng MD5 kim tra cc gi m n phn phi,
trong khi h iu hnh Windows s dng phn mm ca hng th ba.
2/ MD5 c dng m ha mt khu. Mc ch ca vic m ha ny l bin
i mt chui mt khu thnh mt on m khc, sao cho t on m khng
th no ln tr li mt khu. C ngha l vic gii m l khng th hoc phi mt
mt khong thi gian v tn ( lm nn lng cc hacker).
b. Cc ng ha MD5 c dng rng ri trong cc phn mm trn ton th gii
m bo vic truyn tp tin c nguyn vn. V d, my ch tp tin thng
cung cp mt checksum MD5 c tnh ton trc cho tp tin, ngi dng
c th so snh vi checksum ca tp tin ti v. Nhng h iu hnh da trn
nn tng Unix lun km theo tnh nng MD5 sum trong cc gi phn phi ca h,
trong khi ngi dng Windows s dng ng dng ca hng th ba.
c. Tuy nhin, hin nay d dng to ra xung t MD5, mt ngi c th to ra mt
tp tin to ra tp tin th hai vi cng mt checksum, do k thut ny khng
th chng li mt vi dng gi mo nguy him. Ngoi ra, trong mt s trng
hp checksum khng th tin tng c (v d, nu n c ly t mt lnh
nh tp tin ti v), trong trng hp MD5 ch c th c chc nng kim tra
li: n s nhn ra mt li hoc ti v cha xong, rt d xy ra khi ti tp tin ln.
d. MD5 c dng rng ri lu tr mt khu. gim bt s d thng tn
cp trn, ta c th thm salt vo mt khu trc khi bm chng. Mt vi hin
thc c th p dng vo hm bm hn mt ln-xem lm mnh thm kha.
e. -N thng c dng checksum ton b file. Cc nh pht trin ng dng
thng dng MD5 trong vic cho php download file trn NET. H s cho xut
bn mt tn hiu md ca file download. Khi chng ta ti file v , th file chng ta
va download s c mt tn hiu md, nu tn hiu ny khp vi tn hiu cc nh
pht trin ng dng xut bn trn. Th OK, khng c vn . Nu hai tn
hiu md ny khc nhau, c th c trong file download c virut hay ci g
tng t.
-Mt ng dng thng c dng na l hash mt password. c dng cho
vic bo mt mt ng dng, hay nhng g tng t v.v.
II. Tm hiu SHA

1. Gii thiu
a. SHA (Secure Hash Algorithm) l nm thut gii c chp nhn bi
FPS (Federal Information Processing Standards) dng chuyn mt
on d liu nht nh thnh mt on d liu c chiu di khng i
vi xc sut khc bit cao.
C nm thut gii SHA l SHA-1 (tr li kt qu di 160 bit), SHA-224
(tr li kt qu di 224 bit), SHA-256 (tr li kt qu di 256 bit), SHA384 (tr li kt qu di 384 bit), v SHA-512 (tr li kt qu di 512
bit). Thut gii SHA l thut gii bm mt c pht trin bi cc an
ninh quc gia M (National Security Agency hay NSA) v c xut
bn thnh chun ca chnh ph M bi vin cng ngh v chun quc
gia M (National Institute of Standards and Technology hay NIST).
Bn thut gii sau thng c gi chung l SHA-2.
SHA-1 c s dng rng ri trong nhiu ng dng v giao thc an
ninh khc nhau, bao gm TLS v SSL, PGP, SSH, S/MIME, v IPSec.
SHA-1 c coi l thut gii thay th MD5, mt thut gii bm 128 bit
ph bin khc.
Hin nay, SHA-1 khng cn c coi l an ton bi u nm 2005,
ba nh mt m hc ngi Trung Quc pht trin thnh cng mt
thut gii dng tm c hai on d liu nht nh c cng kt
qu bm to ra bi SHA-1. Mc d cha c ai lm c iu tng
t vi SHA-2, nhng v v thut gii, SHA-2 khng khc bit my so
vi SHA-1 nn nhiu nh khoa hc bt u pht trin mt thut
gii khc tt hn SHA. NIST cng khi u mt cuc thi pht trin
thut gii bm mi an ton hn SHA, ging nh quy trnh pht trin
chun m ha tin tin (Advanced Encryption Standard hay AES).
2. 2. SHA-2
SHA-2 bao gm bn gii thut SHA-224, SHA-256, SHA-384 v SHA-512.
Ba thut gii SHA-256, SHA-384 v SHA-512 c xut bn ln u nm
2001 trong bn phc tho FIPS PUB 180-2. Nm 2002, FIPS PUB 180-2,
bao gm c SHA-1 c chp nhn thnh chun chnh thc. Nm 2004,
FIPS PUB 180-2 c b sung thm mt bin th - SHA-224, vi mc ch
to ra mt bin th SHA-2 c di kha trng vi DES ba ln vi 2 kha
(2TDES) - 112 bit. Nhng bin th SHA-2 ny c ng k Bng sng ch
Hoa K s 6.829.355 .

3. M gi ca thut gii SHA-256:

4. Ch : Tt c cc bin u l 32 bit khng du quay vng modulo 232 khi tnh

5.
6. Khi to bin
7. (32 bit u tin ca phn phn s ca cn bc 2 ca 8 s nguyn t u tin
2..19):
8. h0 := 0x6a09e667
9. h1 := 0xbb67ae85
10. h2 := 0x3c6ef372
11. h3 := 0xa54ff53a
12. h4 := 0x510e527f
13. h5 := 0x9b05688c
14. h6 := 0x1f83d9ab
15. h7 := 0x5be0cd19
16.
17. Khi to hng s
18. (32 bit u tin ca phn phn s ca cn bc 3 ca 64 s nguyn t u
tin 2..311):
19. k[0..63] :=
20. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b,
0x59f111f1, 0x923f82a4, 0xab1c5ed5,
21. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74,
0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
22. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
23. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3,
0xd5a79147, 0x06ca6351, 0x14292967,
24. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354,
0x766a0abb, 0x81c2c92e, 0x92722c85,
25. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819,
0xd6990624, 0xf40e3585, 0x106aa070,
26. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3,
0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
27. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa,
0xa4506ceb, 0xbef9a3f7, 0xc67178f2
28.
29. Tin x l:
30. Thm bit '1' vo cui on d liu gc
31. Thm k bit '0', trong k l s nh nht >= 0 sao cho chiu di ca on d
liu gc
32. (tnh bng bit) ng d vi 448 (mod 512)
33. Thm di ca on d liu gc (trc giai on tin x l), tnh bng bits
34. th hin bng mt s 64 bit big endian vo cui on d liu
35.
36. X l on d liu tng 512 bit mt:
37. Tch on d liu ra thnh tng nhm 512 bit
38. Vi mi nhm
39. Tch nhm ra thnh 16 nhm 32 bit big endian w[0..15]

40.
M rng 16 nhm ny thnh 64 nhm 32 bit:
for i t 16 n 63
s0 := (w[i-15] quay vng phi 7) xor (w[i-15] quay vng phi 18) xor
(w[i-15] dch phi 3)
44.
s1 := (w[i-2] quay vng phi 17) xor (w[i-2] quay vng phi 19) xor
(w[i-2] dch phi 10)
45.
w[i] := w[i-16] + s0 + w[i-7] + s1
46.
47. Khi to gi tr bm cho nhm ny:
48. a := h0
49. b := h1
50. c := h2
51. d := h3
52. e := h4
53. f := h5
54. g := h6
55. h := h7
56.
57. Vng lp chnh:
58. for i t 0 n 63
59.
s0 := (a xoay vng phi 2) xor (a xoay vng phi 13) xor (a xoay
vng phi 22)
60.
maj := (a and b) xor (a and c) xor (b and c)
61.
t2 := s0 + maj
62.
s1 := (e xoay vng phi 6) xor (e xoay vng phi 11) xor (e xoay
vng phi 25)
63.
ch := (e and f) xor ((not e) and g)
64.
t1 := h + s1 + ch + k[i] + w[i]
65.
66.
h := g
67.
g := f
68.
f := e
69.
e := d + t1
70.
d := c
71.
c := b
72.
b := a
73.
a := t1 + t2
74.
75. Cng gi tr bm va tnh vo kt qu:
76. h0 := h0 + a
77. h1 := h1 + b
78. h2 := h2 + c
79. h3 := h3 + d
80. h4 := h4 + e
81. h5 := h5 + f
82. h6 := h6 + g
41.
42.
43.

83. h7 := h7 + h
84.
85. To kt qu cui cng (big endian):
86. digest = hash = h0 ni vi h1 ni vi h2 ni vi h3 ni vi h4 ni vi h5
ni vi h6 ni vi h7

87. M ngun SHA-256 bng PHP, tng thch vi UTF-8:


88. <?php
89. /*
90. * Copyright (C) 2006 Hieu Tran Dang (lt2hieu2004)
91. *
92. * This program is free software; you can redistribute it and/or modify it
93. * under the terms of the GNU General Public License as published by the
94. * Free Software Foundation; either version 2 of the License, or (at your
option)
95. * any later version.
96. *
97. * This program is distributed in the hope that it will be useful, but WITHOUT
98. * ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS
99. * FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
100.
*
101.
* You should have received a copy of the GNU General Public
License along with
102.
* this program; if not, write to the Free Software Foundation, Inc.,
59 Temple
103.
* Place, Suite 330, Boston, MA 02111-1307 USA
104.
*/
105.
106.
class Hash
107.
{
108.
function sha256($msg)
109.
{
110.
$msg = Hash::utf8Encode($msg);
111.
$msg .= chr(128);
112.
113.
$l = ceil(strlen($msg) / 4) + 2;
114.
$N = ceil($l / 16);
115.
$M = array();
116.
117.
for ($i = 0; $i < $N; $i++)
118.
{
119.
$M[] = array();
120.
for ($j = 0; $j < 16; $j ++)

121.
{
122.
$M[$i][] = (ord(substr($msg, $i * 64 + $j * 4, 1)) << 24) |
123.
(ord(substr($msg, $i * 64 + $j * 4 + 1, 1)) << 16) |
124.
(ord(substr($msg, $i * 64 + $j * 4 + 2, 1)) << 8) |
125.
(ord(substr($msg, $i * 64 + $j * 4 + 3, 1)));
126.
}
127.
}
128.
129.
$M[$N-1][14] = Hash::SHR((strlen($msg) - 1) * 8, 32);
130.
$M[$N-1][15] = ((strlen($msg) - 1) * 8) & 0xffffffff;
131.
132.
$H = array(
133.
0x6a09e667, 0xbb67ae85,
134.
0x3c6ef372, 0xa54ff53a,
135.
0x510e527f, 0x9b05688c,
136.
0x1f83d9ab, 0x5be0cd19
137.
);
138.
139.
$K = array(
140.
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
141.
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
142.
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
143.
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
144.
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
145.
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
146.
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
147.
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
148.
);
149.
150.
$W = array();
151.
152.
for ($i = 0; $i < $N; $i++)
153.
{
154.
for ($t = 0; $t < 16; $t++) $W[$t] = $M[$i][$t];
155.
for ($t = 16; $t < 64; $t++)
156.
$W[$t] = Hash::sum(Hash::gamma1($W[$t - 2]), $W[$t - 7],
Hash::gamma0($W[$t - 15]), $W[$t - 16]);
157.
158.
$a = $H[0];

159.
$b = $H[1];
160.
$c = $H[2];
161.
$d = $H[3];
162.
$e = $H[4];
163.
$f = $H[5];
164.
$g = $H[6];
165.
$h = $H[7];
166.
167.
for ($t = 0; $t < 64; $t++)
168.
{
169.
$T1 = Hash::sum($h, Hash::sigma1($e), Hash::Ch($e, $f,
$g), $K[$t], $W[$t]);
170.
$T2 = Hash::sum(Hash::sigma0($a), Hash::Maj($a, $b, $c));
171.
$h = $g;
172.
$g = $f;
173.
$f = $e;
174.
$e = Hash::sum($d, $T1);
175.
$d = $c;
176.
$c = $b;
177.
$b = $a;
178.
$a = Hash::sum($T1, $T2);
179.
}
180.
181.
$H[0] = Hash::sum($a, $H[0]);
182.
$H[1] = Hash::sum($b, $H[1]);
183.
$H[2] = Hash::sum($c, $H[2]);
184.
$H[3] = Hash::sum($d, $H[3]);
185.
$H[4] = Hash::sum($e, $H[4]);
186.
$H[5] = Hash::sum($f, $H[5]);
187.
$H[6] = Hash::sum($g, $H[6]);
188.
$H[7] = Hash::sum($h, $H[7]);
189.
}
190.
191.
$hash = "";
192.
for ($i = 0; $i < 8; $i++)
193.
{
194.
$H[$i] = dechex($H[$i]);
195.
while (strlen($H[$i]) < 8) $H[$i] = '0'.$H[$i];
196.
$hash .= $H[$i]
197.
}
198.
199.
return $hash;
200.
}
201.
202.
function gamma0($x)
203.
{

204.
return (Hash::ROTR($x, 7) ^ Hash::ROTR($x, 18) ^
(Hash::SHR($x, 3)));
205.
}
206.
207.
function gamma1($x)
208.
{
209.
return (Hash::ROTR($x, 17) ^ Hash::ROTR($x, 19) ^
(Hash::SHR($x, 10)));
210.
}
211.
212.
function sigma0($x)
213.
{
214.
return (Hash::ROTR($x, 2) ^ Hash::ROTR($x, 13) ^
Hash::ROTR($x, 22));
215.
}
216.
217.
function sigma1($x)
218.
{
219.
return (Hash::ROTR($x, 6) ^ Hash::ROTR($x, 11) ^
Hash::ROTR($x, 25));
220.
}
221.
222.
function Ch($x, $y, $z)
223.
{
224.
return (($x & $y) ^ (~$x & $z));
225.
}
226.
227.
function Maj($x, $y, $z)
228.
{
229.
return (($x & $y) ^ ($x & $z) ^ ($y & $z));
230.
}
231.
232.
function ROTR($x, $n)
233.
{
234.
return (Hash::SHR($x, $n) | ($x << (32 - $n)));
235.
}
236.
237.
function SHR($a, $b)
238.
{
239.
return ($a >> $b) & (pow(2, 32 - $b) - 1);
240.
}
241.
242.
// Hm c vit bi Fyed
243.
function sum()
244.
{
245.
$T = 0;
246.

247.
248.
249.
250.
251.
252.
253.
254.
255.
256.
257.
258.
259.
260.
261.
262.
263.
264.
265.
266.
267.
268.
269.
270.
271.
272.
273.
274.
275.
276.
277.
278.
279.
280.
281.
282.
283.
284.
285.
286.
287.
288.
289.
290.
291.
292.
293.

for($x = 0, $y = func_num_args(); $x < $y; $x++)


{
$a = func_get_arg($x);
$c = 0;
for($i = 0; $i < 32; $i++)
{
$j = (($T >> $i) & 1) + (($a >> $i) & 1) + $c;
$c = ($j >> 1) & 1;
$j &= 1;
$T &= ~(1 << $i);
$T |= $j << $i;
}
}
return $T;
}
// Da trn on code vit bi Angel Martin & Paul Johnston
function utf8Encode($msg)
{
$utfText = "";
for ($n = 0; $n < strlen($msg); $n++)
{
$c = ord(substr($msg, $n, 1));
if ($c < 128)
{
$utfText .= chr($c);
}
else if (($c > 127) && ($c < 2048))
{
$utfText .= chr(($c >> 6) | 192);
$utfText .= chr(($c & 63) | 128);
}
else
{
$utfText .= chr(($c >> 12) | 224);
$utfText .= chr((($c >> 6) & 63) | 128);
$utfText .= chr(($c & 63) | 128);
}
}
return $utfText;
}
}

294.

?>

[sa]V d

295.

Di y l mt s v d s dng thut gii SHA. on d liu gc c

ngm hiu s dng bng m ASCII.


[sa]SHA-1
296.

297.

298.

SHA1("The quick brown fox jumps over the lazy dog")


= 2fd4e1c6 7a2d28fc ed849ee1 bb76e739 1b93eb12
Ngay c mt thay i nh trong on d liu gc s c kh nng rt ln

to nn mt gi tr bm hon ton khc do hiu ng tuyt l. V d,


sa d thnh c:
299.
300.
301.
302.
303.

SHA1("The quick brown fox jumps over the lazy cog")


= de9f2c7f d25e1b3a fad3e85a 0bd17d9b 100db4b3
Gi tr bm ca mt on d liu rng:
SHA1("")
= da39a3ee 5e6b4b0d 3255bfef 95601890 afd80709

[sa]SHA-256
304.
SHA256("The quick brown fox jumps over the lazy dog")
305.
= d7a8fbb3 07d78094 69ca9abc b0082e4f 8d5651e4 6d3cdb76
2d02d0bf 37c9e592
306.

Hiu ng tuyt l khi sa t cui cng thnh "cog":

307.
SHA256("The quick brown fox jumps over the lazy cog")
308.
= e4c4d8f3 bf76b692 de791a17 3e053211 50f7a345 b46484fe
427f6acc 7ecc81be
309.

Gi tr bm ca on d liu rng:

310.
SHA256("")
311.
= e3b0c442 98fc1c14 9afbf4c8 996fb924 27ae41e4 649b934c
a495991b 7852b855

[sa]SHA-512
312.
SHA512("The quick brown fox jumps over the lazy dog")
313.
= 07e547d9 586f6a73 f73fbac0 435ed769 51218fb7 d0c8d788
a309d785 436bbb64
314.
2e93a252 a954f239 12547d1e 8a3b5ed6 e1bfd709 7821233f
a0538f3d b854fee6
315.

Hiu ng tuyt l khi sa t cui cng thnh "cog":

316.
SHA512("The quick brown fox jumps over the lazy cog")
317.
= 3eeee1d0 e11733ef 152a6c29 503b3ae2 0c4f1f3c da4cb26f
1bc1a41f 91c7fe4a
318.
b3bd8649 4049e201 c4bd5155 f31ecb7a 3c860684 3c4cc8df
cab7da11 c8ae5045
319.

Gi tr bm ca on d liu rng:

320.
SHA512("")
321.
= cf83e135 7eefb8bd f1542850 d66d8007 d620e405 0b5715dc
83f4a921 d36ce9ce
322.
47d0d13c 5d85f2b0 ff8318d2 877eec2f 63b931bd 47417a81
a538327a f927da3
323.