5 \brief
\83g
\83\8a\83b
\83v
\82Ì
\90¶
\90¬
7 http://ghanyan.monazilla.org/trip.html
8 \82ð
\8c³
\82É
\83M
\83R
\83i
\83r
\97p
\82É
\88Ú
\90A
\82³
\82¹
\82Ä
\82¢
\82½
\82¾
\82«
\82Ü
\82µ
\82½
\81B
13 SHA1Unit, UBase64, SysUtils, MojuUtils;
17 b_data : array [0..63] of char;
19 PCryptBlock = ^CryptBlock;
21 CryptOrdering = record
22 o_data : array [0..63] of char;
31 \brief
\83g
\83\8a\83b
\83v
\82Ì
\90¶
\90¬
32 \param pw
\8c³
\82É
\82È
\82é
\83p
\83X
\83\8f\81[
\83h
33 \return
\90¶
\90¬
\82³
\82ê
\82½
\83g
\83\8a\83b
\83v
35 function get_2ch_trip(
40 \brief
\83g
\83\8a\83b
\83v
\82Ì
\90¶
\90¬
\82É
\95K
\97v
\82Èsalt
\82Ì
\90¶
\90¬
41 \parm pw salt
\82Ì
\8c³
\82É
\82È
\82é
\83p
\83X
\83\8f\81[
\83h
42 \param salt
\90¶
\90¬
\82µ
\82½salt
\82ª
\8ai
\94[
\82³
\82ê
\82é (array[0..2] of char)
50 kCryptInitialTr : CryptOrdering = ( o_data: (
51 #58,#50,#42,#34,#26,#18,#10, #2,#60,#52,#44,#36,#28,#20,#12, #4,
52 #62,#54,#46,#38,#30,#22,#14, #6,#64,#56,#48,#40,#32,#24,#16, #8,
53 #57,#49,#41,#33,#25,#17, #9, #1,#59,#51,#43,#35,#27,#19,#11, #3,
54 #61,#53,#45,#37,#29,#21,#13, #5,#63,#55,#47,#39,#31,#23,#15, #7
57 kCryptFinalTr : CryptOrdering = ( o_data: (
58 #40, #8,#48,#16,#56,#24,#64,#32,#39, #7,#47,#15,#55,#23,#63,#31,
59 #38, #6,#46,#14,#54,#22,#62,#30,#37, #5,#45,#13,#53,#21,#61,#29,
60 #36, #4,#44,#12,#52,#20,#60,#28,#35, #3,#43,#11,#51,#19,#59,#27,
61 #34, #2,#42,#10,#50,#18,#58,#26,#33, #1,#41, #9,#49,#17,#57,#25
64 kCryptSwap : CryptOrdering = ( o_data: (
65 #33,#34,#35,#36,#37,#38,#39,#40,#41,#42,#43,#44,#45,#46,#47,#48,
66 #49,#50,#51,#52,#53,#54,#55,#56,#57,#58,#59,#60,#61,#62,#63,#64,
67 #1, #2, #3, #4, #5, #6, #7, #8, #9,#10,#11,#12,#13,#14,#15,#16,
68 #17,#18,#19,#20,#21,#22,#23,#24,#25,#26,#27,#28,#29,#30,#31,#32
71 kCryptKeyTr1 : CryptOrdering = ( o_data: (
72 #57, #49, #41, #33, #25, #17, #9, #1, #58, #50, #42, #34, #26, #18,
73 #10, #2, #59, #51, #43, #35, #27, #19, #11, #3, #60, #52, #44, #36,
74 #63, #55, #47, #39, #31, #23, #15, #7, #62, #54, #46, #38, #30, #22,
75 #14, #6, #61, #53, #45, #37, #29, #21, #13, #5, #28, #20, #12, #4,
76 #0, #0, #0, #0, #0, #0, #0, #0
79 kCryptKeyTr2 : CryptOrdering = ( o_data: (
80 #14,#17,#11,#24, #1, #5, #3,#28,#15, #6,#21,#10,
81 #23,#19,#12, #4,#26, #8,#16, #7,#27,#20,#13, #2,
82 #41,#52,#31,#37,#47,#55,#30,#40,#51,#45,#33,#48,
83 #44,#49,#39,#56,#34,#53,#46,#42,#50,#36,#29,#32,
84 #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
88 kCryptEtr : CryptOrdering = ( o_data: (
89 #32, #1, #2, #3, #4, #5, #4, #5, #6, #7, #8, #9,
90 #8, #9,#10,#11,#12,#13,#12,#13,#14,#15,#16,#17,
91 #16,#17,#18,#19,#20,#21,#20,#21,#22,#23,#24,#25,
92 #24,#25,#26,#27,#28,#29,#28,#29,#30,#31,#32, #1,
93 #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
97 kCryptPtr : CryptOrdering = ( o_data: (
98 #16, #7,#20,#21,#29,#12,#28,#17, #1,#15,#23,#26, #5,#18,#31,#10,
99 #2, #8,#24,#14,#32,#27, #3, #9,#19,#13,#30, #6,#22,#11, #4,#25,
100 #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
101 #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0
104 kCryptS_boxes : array [ 0..7, 0..63 ] of Char = (
105 ( #14, #4,#13, #1, #2,#15,#11, #8, #3,#10, #6,#12, #5, #9, #0, #7,
106 #0,#15, #7, #4,#14, #2,#13, #1,#10, #6,#12,#11, #9, #5, #3, #8,
107 #4, #1,#14, #8,#13, #6, #2,#11,#15,#12, #9, #7, #3,#10, #5, #0,
108 #15,#12, #8, #2, #4, #9, #1, #7, #5,#11, #3,#14,#10, #0, #6,#13
111 ( #15, #1, #8,#14, #6,#11, #3, #4, #9, #7, #2,#13,#12, #0, #5,#10,
112 #3,#13, #4, #7,#15, #2, #8,#14,#12, #0, #1,#10, #6, #9,#11, #5,
113 #0,#14, #7,#11,#10, #4,#13, #1, #5, #8,#12, #6, #9, #3, #2,#15,
114 #13, #8,#10, #1, #3,#15, #4, #2,#11, #6, #7,#12, #0, #5,#14, #9
117 ( #10, #0, #9,#14, #6, #3,#15, #5, #1,#13,#12, #7,#11, #4, #2, #8,
118 #13, #7, #0, #9, #3, #4, #6,#10, #2, #8, #5,#14,#12,#11,#15, #1,
119 #13, #6, #4, #9, #8,#15, #3, #0,#11, #1, #2,#12, #5,#10,#14, #7,
120 #1,#10,#13, #0, #6, #9, #8, #7, #4,#15,#14, #3,#11, #5, #2,#12
123 ( #7,#13,#14, #3, #0, #6, #9,#10, #1, #2, #8, #5,#11,#12, #4,#15,
124 #13, #8,#11, #5, #6,#15, #0, #3, #4, #7, #2,#12, #1,#10,#14, #9,
125 #10, #6, #9, #0,#12,#11, #7,#13,#15, #1, #3,#14, #5, #2, #8, #4,
126 #3,#15, #0, #6,#10, #1,#13, #8, #9, #4, #5,#11,#12, #7, #2,#14
129 ( #2,#12, #4, #1, #7,#10,#11, #6, #8, #5, #3,#15,#13, #0,#14, #9,
130 #14,#11, #2,#12, #4, #7,#13, #1, #5, #0,#15,#10, #3, #9, #8, #6,
131 #4, #2, #1,#11,#10,#13, #7, #8,#15, #9,#12, #5, #6, #3, #0,#14,
132 #11, #8,#12, #7, #1,#14, #2,#13, #6,#15, #0, #9,#10, #4, #5, #3
135 ( #12, #1,#10,#15, #9, #2, #6, #8, #0,#13, #3, #4,#14, #7, #5,#11,
136 #10,#15, #4, #2, #7,#12, #9, #5, #6, #1,#13,#14, #0,#11, #3, #8,
137 #9,#14,#15, #5, #2, #8,#12, #3, #7, #0, #4,#10, #1,#13,#11, #6,
138 #4, #3, #2,#12, #9, #5,#15,#10,#11,#14, #1, #7, #6, #0, #8,#13
141 ( #4,#11, #2,#14,#15, #0, #8,#13, #3,#12, #9, #7, #5,#10, #6, #1,
142 #13, #0,#11, #7, #4, #9, #1,#10,#14, #3, #5,#12, #2,#15, #8, #6,
143 #1, #4,#11,#13,#12, #3, #7,#14,#10,#15, #6, #8, #0, #5, #9, #2,
144 #6,#11,#13, #8, #1, #4,#10, #7, #9, #5, #0,#15,#14, #2, #3,#12
147 ( #13, #2, #8, #4, #6,#15,#11, #1,#10, #9, #3,#14, #5, #0,#12, #7,
148 #1,#15,#13, #8,#10, #3, #7, #4,#12, #5, #6,#11, #0,#14, #9, #2,
149 #7,#11, #4, #1, #9,#12,#14, #2, #0, #6,#10,#13,#15, #3, #5, #8,
150 #2, #1,#14, #7, #4,#10, #8,#13,#15,#12, #9, #0, #3, #5, #6,#11
154 kCryptRots : array [ 0..15 ] of Integer = (
155 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
161 var data : CryptBlock;
162 var t : CryptOrdering;
173 data.b_data[ n ] := x.b_data[ Integer( t.o_data[ n ] ) - 1 ];
183 data0, data28 : Char;
187 ep := @(key.b_data[ 55 ]);
188 data0 := key.b_data[ 0 ];
189 data28 := key.b_data[ 28 ];
196 key.b_data[ 27 ] := data0;
197 key.b_data[ 55 ] := data28;
203 var key : CryptBlock;
209 e, ikey, y : CryptBlock;
215 temp : CryptOrdering;
219 transpose( e, data.EP^, 48 );
220 for k := kCryptRots[ i ] downto 1
223 temp := kCryptKeyTr2; transpose( ikey, temp, 48 );
224 p := @(y.b_data[ 48 ]);
225 q := @(e.b_data[ 48 ]);
226 r := @(ikey.b_data[ 48 ]);
227 while p > y.b_data do
232 p^ := Char( Integer( q^ ) xor Integer( r^ ) );
237 ir := Integer( p^ ) shl 5; Inc( p );
238 ir := ir + Integer( p^ ) shl 3; Inc( p );
239 ir := ir + Integer( p^ ) shl 2; Inc( p );
240 ir := ir + Integer( p^ ) shl 1; Inc( p );
241 ir := ir + Integer( p^ ); Inc( p );
242 ir := ir + Integer( p^ ) shl 4; Inc( p );
244 xb := Integer( kCryptS_Boxes[ k, ir ] );
246 q^ := Char( (xb shr 3) and 1 ); Inc( q );
247 q^ := Char( (xb shr 2) and 1 ); Inc( q );
248 q^ := Char( (xb shr 1) and 1 ); Inc( q );
249 q^ := Char( xb and 1 ); Inc( q );
251 temp := kCryptPtr; transpose( x, temp, 32 );
260 temp : CryptOrdering;
263 Move( Pointer( k )^, data.Key.b_data, SizeOf(CryptBlock) );
264 temp := kCryptKeyTr1; transpose( data.Key, temp, 56 );
282 temp : CryptOrdering;
286 p := PCryptBlock( blck );
288 temp := kCryptInitialTr;transpose( p^, temp, 64 );
289 for i := 15 downto 0 do
298 do p^.b_data[ k ] := b.b_data[ k + 32 ];
299 f( j, key^, p^, x, data );
301 do p^.b_data[ k + 32 ] := Char( Integer( b.b_data[ k ] ) xor Integer( x.b_data[ k ] ) );
303 temp := kCryptSwap; transpose( p^, temp, 64 );
304 temp := kCryptFinalTr; transpose( p^, temp, 64 );
314 pwb : array [0..65] of char;
316 ret : array [0..15] of char;
318 new_etr : CryptOrdering;
328 data.EP := @kCryptEtr;
329 while (pw^ <> #0) and (p < pwb + 64) do
336 p^ := Char( (Integer(pw^) shr j) and 1 );
345 while (p < pwb + 64) do
354 while (p < pwb + 66) do
360 new_etr := kCryptEtr;
362 if (salt[ 0 ] = #0) and (salt[ 1 ] = #0) then
371 c := Char( Integer(c) - (6 + 7 + Integer('.')) )
373 c := Char( Integer(c) - (7 + Integer('.')) )
375 c := Char( (Integer(c) - Integer('.')) and $ff );
379 if ((Integer(c) shr j) and 1) <> 0 then
382 temp := Integer( new_etr.o_data[ t ] );
383 new_etr.o_data[ t ] := new_etr.o_data[ t + 24 ];
384 new_etr.o_data[ t + 24 ] := Char( temp );
389 if ret[ 1 ] = #0 then
390 ret[ 1 ] := ret[ 0 ];
393 encrypt_r( pwb, 0, data );
394 data.EP := @kCryptEtr;
398 while p < pwb + 66 do
406 c := Char( (Integer(c) shl 1) or Integer(p^) );
410 c := Char( Integer(c) + Integer('.') );
412 c := Char( Integer(c) + 7 );
414 c := Char( Integer(c) + 6 );
423 procedure get_pw_salt(
431 // ^([0-9A-Fa-f]{16})([./0-9A-Za-z]{0,2})$
432 if (Length(pw) >= 17) and (Length(pw) <= 19) then begin
433 //
\83L
\81[
\95\94\95ª
434 for i := 0 to 7 do begin
435 if (Pos(pw[2*i + 0 + 1], '0123456789abcdefABCDEF') > 0) and
436 (Pos(pw[2*i + 1 + 1], '0123456789abcdefABCDEF') > 0) then begin
438 Char(StrToInt( 'x' + pw[2*i + 0 + 1] ) shl 4 + StrToInt( 'x' + pw[2*i + 1 + 1] ));
445 if (Length(convpw) = 8) then begin
446 if (Length(pw) = 19) then begin
447 if (Pos(pw[17], './0123456789abcdefABCDEF') > 0) and
448 (Pos(pw[18], './0123456789abcdefABCDEF') > 0) then begin
455 end else if (Length(pw) = 18) then begin
456 if (Pos(pw[17], './0123456789abcdefABCDEF') > 0) then begin
472 function get_2ch_trip(
477 salt : array [0..2] of char;
478 digest : TSHA1Digest;
486 // 11
\8c\85\82Ü
\82Å
\82Í
\8b\8c\95û
\8e®
487 if (Length(pw) <= 11) then begin
488 get_salt( pw, salt );
489 Result := Copy( crypt_r( pw, salt, s ), 4, 100 );
491 //
\90V
\95û
\8e®
\83g
\83\8a\83b
\83v
492 if pw[ 0 ] = '$' then begin
493 //
\8f«
\97\88\82Ì
\8ag
\92£
\97p
497 //
\90¶
\83L
\81[
\95û
\8e®
498 if pw[ 0 ] = '#' then begin
499 get_pw_salt(pw, convpw, salt);
501 if (pw[ 0 ] = '#') and (Length(pw) >= 20) then begin
502 //
\8f«
\97\88\82Ì
\8ag
\92£
\97p
504 end else if Length(convpw) = 0 then begin
505 //
\90¶
\83L
\81[
\95û
\8e®
\83G
\83\89\81[
507 end else if Length(convpw) = 8 then begin
508 Result := Copy( crypt_r( PChar(convpw), salt, s ), 4, 100 );
511 StringHashSHA1(digest, pw);
512 Result := Copy(HogeBase64Encode(digest), 0, 12);
513 Result := MojuUtils.CustomStringReplace(Result, '+', '.');
528 if pw[ 1 ] <> #0 then
530 if pw[ 2 ] <> #0 then
534 for i := 0 to len - 1 do
536 if ('.' <= pw[ i + 1 ]) and (pw[ i + 1 ] <= 'z' ) then
537 salt[ i ] := pw[ i + 1 ]
541 if Pos ( salt[ i ], ':;<=>?@' ) > 0 then begin
542 salt[ i ] := Char( Integer( salt[ i ] ) + 7 );
543 end else if Pos( salt[ i ], '[\\]^_`' ) > 0 then begin
544 salt[ i ] := Char( Integer( salt[ i ] ) + 6 );