File: Hasher.cs

package info (click to toggle)
banshee 2.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 47,208 kB
  • sloc: xml: 163,694; cs: 137,409; sh: 11,650; ansic: 4,790; makefile: 2,922; python: 38
file content (219 lines) | stat: -rw-r--r-- 8,345 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * daap-sharp
 * Copyright (C) 2005  James Willcox <snorp@snorp.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
/* Copyright (C) 2004 David Hammerton <david@crazney.net>
 * Copyright (C) 2005 Jon Lech Johansen <jon@nanocrew.net>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Text;

namespace Daap {

    internal class Hasher
    {
        private static byte [] _hasht42 = null;
        private static byte [] _hasht45 = null;

        private static byte [] _hexchars =
        Encoding.ASCII.GetBytes( "0123456789ABCDEF" );
        private static byte [] _copyright = Convert.FromBase64String(
                                                                     "Q29weXJpZ2h0IDIwMDMgQXBwbGUgQ29tcHV0ZXIsIEluYy4=" );

        private static void HashToString( byte [] hash, byte [] str, int offset )
        {
            for( int i = 0; i < hash.Length; i++ )
            {
                byte tmp = hash[ i ];
                str[ i * 2 + 1 + offset ] = _hexchars[ tmp & 0xF ];
                str[ i * 2 + 0 + offset ] = _hexchars[ (tmp >> 4) & 0xF ];
            }
        }

        private static void TransformString( BrokenMD5 md5, string str, bool final )
        {
            byte [] tmp = Encoding.ASCII.GetBytes( str );

            if( final )
                md5.TransformFinalBlock( tmp, 0, tmp.Length );
            else
                md5.TransformBlock( tmp, 0, tmp.Length, tmp, 0 );
        }

        private static void GenerateTable42()
        {
            int i;

            _hasht42 = new byte[ 256 * 32 ];

            for( i = 0; i < 256; i++ )
            {
                BrokenMD5 md5 = new BrokenMD5( 0 );

                if( ( i & 0x80 ) != 0 )
                    TransformString( md5, "Accept-Language", false );
                else
                    TransformString( md5, "user-agent", false );

                if( ( i & 0x40 ) != 0 )
                    TransformString( md5, "max-age", false );
                else
                    TransformString( md5, "Authorization", false );

                if( ( i & 0x20 ) != 0 )
                    TransformString( md5, "Client-DAAP-Version", false );
                else
                    TransformString( md5, "Accept-Encoding", false );

                if( ( i & 0x10 ) != 0 )
                    TransformString( md5, "daap.protocolversion", false );
                else
                    TransformString( md5, "daap.songartist", false );

                if( ( i & 0x08 ) != 0 )
                    TransformString( md5, "daap.songcomposer", false );
                else
                    TransformString( md5, "daap.songdatemodified", false );

                if( ( i & 0x04 ) != 0 )
                    TransformString( md5, "daap.songdiscnumber", false );
                else
                    TransformString( md5, "daap.songdisabled", false );

                if( ( i & 0x02 ) != 0 )
                    TransformString( md5, "playlist-item-spec", false );
                else
                    TransformString( md5, "revision-number", false );

                if( ( i & 0x01 ) != 0 )
                    TransformString( md5, "session-id", true );
                else
                    TransformString( md5, "content-codes", true );

                HashToString( md5.Hash, _hasht42, i * 32 );
            }
        }

        private static void GenerateTable45()
        {
            int i;

            _hasht45 = new byte[ 256 * 32 ];

            for( i = 0; i < 256; i++ )
            {
                BrokenMD5 md5 = new BrokenMD5( 1 );

                if( ( i & 0x40 ) != 0 )
                    TransformString( md5, "eqwsdxcqwesdc", false );
                else
                    TransformString( md5, "op[;lm,piojkmn", false );

                if( ( i & 0x20 ) != 0 )
                    TransformString( md5, "876trfvb 34rtgbvc", false );
                else
                    TransformString( md5, "=-0ol.,m3ewrdfv", false );

                if( ( i & 0x10 ) != 0 )
                    TransformString( md5, "87654323e4rgbv ", false );
                else
                    TransformString( md5, "1535753690868867974342659792", false );

                if( ( i & 0x08 ) != 0 )
                    TransformString( md5, "Song Name", false );
                else
                    TransformString( md5, "DAAP-CLIENT-ID:", false );

                if( ( i & 0x04 ) != 0 )
                    TransformString( md5, "111222333444555", false );
                else
                    TransformString( md5, "4089961010", false );

                if( ( i & 0x02 ) != 0 )
                    TransformString( md5, "playlist-item-spec", false );
                else
                    TransformString( md5, "revision-number", false );

                if( ( i & 0x01 ) != 0 )
                    TransformString( md5, "session-id", false );
                else
                    TransformString( md5, "content-codes", false );

                if( ( i & 0x80 ) != 0 )
                    TransformString( md5, "IUYHGFDCXWEDFGHN", true );
                else
                    TransformString( md5, "iuytgfdxwerfghjm", true );

                HashToString( md5.Hash, _hasht45, i * 32 );
            }
        }

        public static string GenerateHash(int version_major, string url,
                                          int hash_select, int request_id )
        {
            if( _hasht42 == null )
                GenerateTable42();
            if( _hasht45 == null )
                GenerateTable45();

            byte [] hashtable = (version_major == 3) ? _hasht45 : _hasht42;
            BrokenMD5 md5 = new BrokenMD5( (version_major == 3) ? 1 : 0 );
            byte [] hash = new byte[ 32 ];

            byte [] tmp = Encoding.ASCII.GetBytes( url );
            md5.TransformBlock( tmp, 0, tmp.Length, tmp, 0 );
            md5.TransformBlock( _copyright, 0, _copyright.Length, _copyright, 0 );

            if( request_id > 0 && version_major == 3 )
            {
                md5.TransformBlock( hashtable, hash_select * 32, 32,
                                    hashtable, hash_select * 32 );
                tmp = Encoding.ASCII.GetBytes( request_id.ToString() );
                md5.TransformFinalBlock( tmp, 0, tmp.Length );
            }
            else
            {
                md5.TransformFinalBlock( hashtable, hash_select * 32, 32 );
            }

            HashToString( md5.Hash, hash, 0 );

            return Encoding.ASCII.GetString( hash );
        }
    }
}