OSDN Git Service

2ちゃんねるのトリップ仕様の拡張に対応
[gikonavigoeson/gikonavi.git] / Trip.pas
index 0c103f6..1c2a78a 100644 (file)
--- a/Trip.pas
+++ b/Trip.pas
@@ -9,6 +9,9 @@ unit Trip;
 }
 interface
 
+uses
+    SHA1Unit, UBase64, SysUtils;
+
 type
        CryptBlock = record
                b_data : array [0..63] of char;
@@ -33,6 +36,16 @@ function get_2ch_trip(
        const pw : PChar
 ) : string;
 
+{!
+\brief  \83g\83\8a\83b\83v\82Ì\90\90¬\82É\95K\97v\82Èsalt\82Ì\90\90¬
+\parm   pw  salt\82Ì\8c³\82É\82È\82é\83p\83X\83\8f\81[\83h
+\param  salt    \90\90¬\82µ\82½salt\82ª\8ai\94[\82³\82ê\82é (array[0..2] of char)
+}
+procedure get_salt(
+    const pw : PChar;
+    salt : PChar
+);
+
 const
        kCryptInitialTr : CryptOrdering = ( o_data: (
                #58,#50,#42,#34,#26,#18,#10, #2,#60,#52,#44,#36,#28,#20,#12, #4,
@@ -407,23 +420,103 @@ begin
 
 end;
 
+procedure get_pw_salt(
+    const pw : PChar;
+    var convpw : String;
+    const salt : PChar
+);
+var
+    i : integer;
+begin
+    // ^([0-9A-Fa-f]{16})([./0-9A-Za-z]{0,2})$
+    if (Length(pw) >= 17) or (Length(pw) <= 19) then begin
+        // \83L\81[\95\94\95ª
+        for  i := 0 to 7 do begin
+            if (Pos(pw[2*i + 0 + 1], '0123456789abcdefABCDEF') > 0) and
+                (Pos(pw[2*i + 1 + 1], '0123456789abcdefABCDEF') > 0) then begin
+                convpw := convpw +
+                    Char(StrToInt( pw[2*i + 0 + 1] ) shl 4 + StrToInt( pw[2*i + 1 + 1] ));
+            end else begin
+                convpw := '';
+                Break;
+            end;
+        end;
+
+        if (Length(convpw) = 8) then begin
+            if (Length(pw) = 19) then begin
+                if (Pos(pw[17], './0123456789abcdefABCDEF') > 0) and
+                    (Pos(pw[18], './0123456789abcdefABCDEF') > 0) then begin
+                    salt[ 0 ] := pw[17];
+                    salt[ 1 ] := pw[18];
+                    salt[ 2 ] := #0;
+                end else begin
+                    convpw := '';
+                end;
+            end else if (Length(pw) = 18) then begin
+                if (Pos(pw[17], './0123456789abcdefABCDEF') > 0) then begin
+                    salt[ 0 ] := pw[17];
+                    salt[ 1 ] := '.';
+                    salt[ 2 ] := #0;
+                end else begin
+                    convpw := '';
+                end;
+            end else begin
+                salt[ 0 ] := '.';
+                salt[ 1 ] := '.';
+                salt[ 2 ] := #0;
+            end;
+        end;
+    end;
+end;
+
 function get_2ch_trip(
        const pw : PChar
 ) : string;
 var
        s : CryptData;
        salt : array [0..2] of char;
-
-       i : Integer;
-       len : Integer;
+    digest : TSHA1Digest;
+    convpw : String;
 begin
-
-       salt[ 0 ] := #0;
+    Result := '';
        if pw[ 0 ] = #0 then
        begin
-               Result := '';
                Exit;
        end;
+    // 11\8c\85\82Ü\82Å\82Í\8b\8c\95û\8e®
+    if (Length(pw) <= 11) then begin
+        get_salt( pw, salt );
+       Result := Copy( crypt_r( pw, salt, s ), 4, 100 );
+    end else begin
+        // \90V\95û\8e®\83g\83\8a\83b\83v
+        if pw[ 0 ] = '$' then begin
+            // \8f«\97\88\82Ì\8ag\92£\97p
+            Result := '???';
+        end else begin
+            convpw := '';
+            // \90\83L\81[\95û\8e®
+            if pw[ 0 ] = '#' then begin
+                get_pw_salt(pw, convpw, salt);
+            end;
+            if Length(convpw) = 8 then begin
+                Result := Copy( crypt_r( PChar(convpw), salt, s ), 4, 100 );
+            end else begin
+                // \90V\95û\8e®
+                StringHashSHA1(digest, pw);
+                Result := Copy(HogeBase64Encode(digest), 0, 12);
+            end;
+        end;
+    end;
+end;
+
+procedure get_salt(
+    const pw : PChar;
+    salt : PChar
+);
+var
+    i, len : Integer;
+begin
+       salt[ 0 ] := #0;
 
        if pw[ 1 ] <> #0 then
        begin
@@ -438,8 +531,11 @@ begin
                        else
                                salt[ i ] := '.';
 
-                       if Pos( salt[ i ], ':;<=>?@[\\]^_`' ) > 0 then
-                               salt[ i ] := Char( Integer( salt[ i ] ) + 7 );
+            if Pos ( salt[ i ], ':;<=>?@' ) > 0 then begin
+                salt[ i ] := Char( Integer( salt[ i ] ) + 7 );
+            end else if Pos( salt[ i ], '[\\]^_`' ) > 0 then begin
+                               salt[ i ] := Char( Integer( salt[ i ] ) + 6 );
+            end;
                end;
                if len = 1 then
                        salt[ 1 ] := 'H';
@@ -448,9 +544,6 @@ begin
                salt[ 0 ] := 'H';
                salt[ 1 ] := '.';
        end;
-
-       Result := Copy( crypt_r( pw, salt, s ), 4, 100 );
-
 end;
 
 end.