]>
Commit | Line | Data |
---|---|---|
256c9cb8 JB |
1 | --- chronojump-1.7.0/chronojump_server.orig/BCrypt.cs 1970-01-01 01:00:00.000000000 +0100 |
2 | +++ chronojump-1.7.0/chronojump_server/BCrypt.cs 2017-02-12 08:49:53.687074884 +0100 | |
3 | @@ -0,0 +1,760 @@ | |
4 | +// Copyright (c) 2006 Damien Miller <djm@mindrot.org> | |
5 | +// Copyright (c) 2007 Derek Slager | |
6 | +// | |
7 | +// Permission to use, copy, modify, and distribute this software for any | |
8 | +// purpose with or without fee is hereby granted, provided that the above | |
9 | +// copyright notice and this permission notice appear in all copies. | |
10 | +// | |
11 | +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
12 | +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
13 | +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
14 | +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
15 | +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
16 | +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
17 | +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
18 | + | |
19 | +using System; | |
20 | +using System.Collections.Generic; | |
21 | +using System.Globalization; | |
22 | +using System.Security.Cryptography; | |
23 | +using System.Text; | |
24 | + | |
25 | +[assembly: System.Reflection.AssemblyVersion("0.1")] | |
26 | + | |
27 | + | |
28 | +//http://derekslager.com/blog/posts/2007/10/bcrypt-dotnet-strong-password-hashing-for-dotnet-and-mono.ashx | |
29 | + | |
30 | +/// <summary>BCrypt implements OpenBSD-style Blowfish password hashing | |
31 | +/// using the scheme described in "A Future-Adaptable Password Scheme" | |
32 | +/// by Niels Provos and David Mazieres.</summary> | |
33 | +/// <remarks> | |
34 | +/// <para>This password hashing system tries to thwart offline | |
35 | +/// password cracking using a computationally-intensive hashing | |
36 | +/// algorithm, based on Bruce Schneier's Blowfish cipher. The work | |
37 | +/// factor of the algorithm is parametized, so it can be increased as | |
38 | +/// computers get faster.</para> | |
39 | +/// <para>To hash a password for the first time, call the | |
40 | +/// <c>HashPassword</c> method with a random salt, like this:</para> | |
41 | +/// <code> | |
42 | +/// string hashed = BCrypt.HashPassword(plainPassword, BCrypt.GenerateSalt()); | |
43 | +/// </code> | |
44 | +/// <para>To check whether a plaintext password matches one that has | |
45 | +/// been hashed previously, use the <c>CheckPassword</c> method:</para> | |
46 | +/// <code> | |
47 | +/// if (BCrypt.CheckPassword(candidatePassword, storedHash)) { | |
48 | +/// Console.WriteLine("It matches"); | |
49 | +/// } else { | |
50 | +/// Console.WriteLine("It does not match"); | |
51 | +/// } | |
52 | +/// </code> | |
53 | +/// <para>The <c>GenerateSalt</c> method takes an optional parameter | |
54 | +/// (logRounds) that determines the computational complexity of the | |
55 | +/// hashing:</para> | |
56 | +/// <code> | |
57 | +/// string strongSalt = BCrypt.GenerateSalt(10); | |
58 | +/// string strongerSalt = BCrypt.GenerateSalt(12); | |
59 | +/// </code> | |
60 | +/// <para> | |
61 | +/// The amount of work increases exponentially (2**log_rounds), so | |
62 | +/// each increment is twice as much work. The default log_rounds is | |
63 | +/// 10, and the valid range is 4 to 31. | |
64 | +/// </para> | |
65 | +/// </remarks> | |
66 | +public class BCrypt { | |
67 | + | |
68 | + private const int GENSALT_DEFAULT_LOG2_ROUNDS = 10; | |
69 | + private const int BCRYPT_SALT_LEN = 16; | |
70 | + | |
71 | + // Blowfish parameters. | |
72 | + private const int BLOWFISH_NUM_ROUNDS = 16; | |
73 | + | |
74 | + // Initial contents of key schedule. | |
75 | + private static readonly uint[] p_orig = { | |
76 | + 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, | |
77 | + 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, | |
78 | + 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, | |
79 | + 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, | |
80 | + 0x9216d5d9, 0x8979fb1b | |
81 | + }; | |
82 | + | |
83 | + private static readonly uint[] s_orig = { | |
84 | + 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, | |
85 | + 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, | |
86 | + 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, | |
87 | + 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, | |
88 | + 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, | |
89 | + 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, | |
90 | + 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, | |
91 | + 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, | |
92 | + 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, | |
93 | + 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, | |
94 | + 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, | |
95 | + 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, | |
96 | + 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, | |
97 | + 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, | |
98 | + 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, | |
99 | + 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, | |
100 | + 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, | |
101 | + 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, | |
102 | + 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, | |
103 | + 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, | |
104 | + 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, | |
105 | + 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, | |
106 | + 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, | |
107 | + 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, | |
108 | + 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, | |
109 | + 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, | |
110 | + 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, | |
111 | + 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, | |
112 | + 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, | |
113 | + 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, | |
114 | + 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, | |
115 | + 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, | |
116 | + 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, | |
117 | + 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, | |
118 | + 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, | |
119 | + 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, | |
120 | + 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, | |
121 | + 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, | |
122 | + 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, | |
123 | + 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, | |
124 | + 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, | |
125 | + 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, | |
126 | + 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, | |
127 | + 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, | |
128 | + 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, | |
129 | + 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, | |
130 | + 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, | |
131 | + 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, | |
132 | + 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, | |
133 | + 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, | |
134 | + 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, | |
135 | + 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, | |
136 | + 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, | |
137 | + 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, | |
138 | + 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, | |
139 | + 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, | |
140 | + 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, | |
141 | + 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, | |
142 | + 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, | |
143 | + 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, | |
144 | + 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, | |
145 | + 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, | |
146 | + 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, | |
147 | + 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a, | |
148 | + 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, | |
149 | + 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, | |
150 | + 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, | |
151 | + 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, | |
152 | + 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, | |
153 | + 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, | |
154 | + 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, | |
155 | + 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, | |
156 | + 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, | |
157 | + 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, | |
158 | + 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, | |
159 | + 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, | |
160 | + 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, | |
161 | + 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, | |
162 | + 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, | |
163 | + 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, | |
164 | + 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, | |
165 | + 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, | |
166 | + 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, | |
167 | + 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, | |
168 | + 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, | |
169 | + 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, | |
170 | + 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, | |
171 | + 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, | |
172 | + 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, | |
173 | + 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, | |
174 | + 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, | |
175 | + 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, | |
176 | + 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, | |
177 | + 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, | |
178 | + 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, | |
179 | + 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, | |
180 | + 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, | |
181 | + 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, | |
182 | + 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, | |
183 | + 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, | |
184 | + 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, | |
185 | + 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, | |
186 | + 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, | |
187 | + 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, | |
188 | + 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, | |
189 | + 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, | |
190 | + 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, | |
191 | + 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, | |
192 | + 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, | |
193 | + 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, | |
194 | + 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, | |
195 | + 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, | |
196 | + 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, | |
197 | + 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, | |
198 | + 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, | |
199 | + 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, | |
200 | + 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, | |
201 | + 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, | |
202 | + 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, | |
203 | + 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, | |
204 | + 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, | |
205 | + 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, | |
206 | + 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, | |
207 | + 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, | |
208 | + 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, | |
209 | + 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, | |
210 | + 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, | |
211 | + 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7, | |
212 | + 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, | |
213 | + 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, | |
214 | + 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, | |
215 | + 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, | |
216 | + 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, | |
217 | + 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, | |
218 | + 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, | |
219 | + 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, | |
220 | + 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, | |
221 | + 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, | |
222 | + 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, | |
223 | + 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, | |
224 | + 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, | |
225 | + 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, | |
226 | + 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, | |
227 | + 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, | |
228 | + 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, | |
229 | + 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, | |
230 | + 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, | |
231 | + 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, | |
232 | + 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, | |
233 | + 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, | |
234 | + 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, | |
235 | + 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, | |
236 | + 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, | |
237 | + 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, | |
238 | + 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, | |
239 | + 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, | |
240 | + 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, | |
241 | + 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, | |
242 | + 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, | |
243 | + 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, | |
244 | + 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, | |
245 | + 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, | |
246 | + 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, | |
247 | + 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, | |
248 | + 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, | |
249 | + 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, | |
250 | + 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, | |
251 | + 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, | |
252 | + 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, | |
253 | + 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, | |
254 | + 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, | |
255 | + 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, | |
256 | + 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, | |
257 | + 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, | |
258 | + 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, | |
259 | + 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, | |
260 | + 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, | |
261 | + 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, | |
262 | + 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, | |
263 | + 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, | |
264 | + 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, | |
265 | + 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, | |
266 | + 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, | |
267 | + 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, | |
268 | + 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, | |
269 | + 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, | |
270 | + 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, | |
271 | + 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, | |
272 | + 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, | |
273 | + 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, | |
274 | + 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, | |
275 | + 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0, | |
276 | + 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, | |
277 | + 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, | |
278 | + 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, | |
279 | + 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, | |
280 | + 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, | |
281 | + 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, | |
282 | + 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, | |
283 | + 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, | |
284 | + 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, | |
285 | + 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, | |
286 | + 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, | |
287 | + 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, | |
288 | + 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, | |
289 | + 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, | |
290 | + 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, | |
291 | + 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, | |
292 | + 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, | |
293 | + 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, | |
294 | + 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, | |
295 | + 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, | |
296 | + 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, | |
297 | + 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, | |
298 | + 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, | |
299 | + 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, | |
300 | + 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, | |
301 | + 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, | |
302 | + 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, | |
303 | + 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, | |
304 | + 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, | |
305 | + 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, | |
306 | + 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, | |
307 | + 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, | |
308 | + 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, | |
309 | + 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, | |
310 | + 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, | |
311 | + 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, | |
312 | + 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, | |
313 | + 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, | |
314 | + 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, | |
315 | + 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, | |
316 | + 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, | |
317 | + 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, | |
318 | + 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, | |
319 | + 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, | |
320 | + 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, | |
321 | + 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, | |
322 | + 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, | |
323 | + 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, | |
324 | + 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, | |
325 | + 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, | |
326 | + 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, | |
327 | + 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, | |
328 | + 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, | |
329 | + 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, | |
330 | + 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, | |
331 | + 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, | |
332 | + 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, | |
333 | + 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, | |
334 | + 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, | |
335 | + 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, | |
336 | + 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, | |
337 | + 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, | |
338 | + 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, | |
339 | + 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 | |
340 | + }; | |
341 | + | |
342 | + // bcrypt IV: "OrpheanBeholderScryDoubt". | |
343 | + private static readonly uint[] bf_crypt_ciphertext = { | |
344 | + 0x4f727068, 0x65616e42, 0x65686f6c, | |
345 | + 0x64657253, 0x63727944, 0x6f756274 | |
346 | + }; | |
347 | + | |
348 | + // Table for Base64 encoding. | |
349 | + private static readonly char[] base64_code = { | |
350 | + '.', '/', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', | |
351 | + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', | |
352 | + 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', | |
353 | + 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', | |
354 | + 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', | |
355 | + '6', '7', '8', '9' | |
356 | + }; | |
357 | + | |
358 | + // Table for Base64 decoding. | |
359 | + private static readonly int[] index_64 = { | |
360 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
361 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
362 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
363 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
364 | + -1, -1, -1, -1, -1, -1, 0, 1, 54, 55, | |
365 | + 56, 57, 58, 59, 60, 61, 62, 63, -1, -1, | |
366 | + -1, -1, -1, -1, -1, 2, 3, 4, 5, 6, | |
367 | + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | |
368 | + 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, | |
369 | + -1, -1, -1, -1, -1, -1, 28, 29, 30, | |
370 | + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
371 | + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, | |
372 | + 51, 52, 53, -1, -1, -1, -1, -1 | |
373 | + }; | |
374 | + | |
375 | + // Expanded Blowfish key. | |
376 | + private uint[] p; | |
377 | + private uint[] s; | |
378 | + | |
379 | + /// <summary>Encode a byte array using bcrypt's slightly-modified | |
380 | + /// Base64 encoding scheme. Note that this is _not_ compatible | |
381 | + /// with the standard MIME-Base64 encoding.</summary> | |
382 | + /// <param name="d">The byte array to encode</param> | |
383 | + /// <param name="length">The number of bytes to encode</param> | |
384 | + /// <returns>A Base64-encoded string</returns> | |
385 | + private static string EncodeBase64(byte[] d, int length) { | |
386 | + | |
387 | + if (length <= 0 || length > d.Length) { | |
388 | + throw new ArgumentOutOfRangeException("length", length, null); | |
389 | + } | |
390 | + | |
391 | + StringBuilder rs = new StringBuilder(length * 2); | |
392 | + | |
393 | + for (int offset = 0, c1, c2; offset < length; ) { | |
394 | + c1 = d[offset++] & 0xff; | |
395 | + rs.Append(base64_code[(c1 >> 2) & 0x3f]); | |
396 | + c1 = (c1 & 0x03) << 4; | |
397 | + if (offset >= length) { | |
398 | + rs.Append(base64_code[c1 & 0x3f]); | |
399 | + break; | |
400 | + } | |
401 | + c2 = d[offset++] & 0xff; | |
402 | + c1 |= (c2 >> 4) & 0x0f; | |
403 | + rs.Append(base64_code[c1 & 0x3f]); | |
404 | + c1 = (c2 & 0x0f) << 2; | |
405 | + if (offset >= length) { | |
406 | + rs.Append(base64_code[c1 & 0x3f]); | |
407 | + break; | |
408 | + } | |
409 | + c2 = d[offset++] & 0xff; | |
410 | + c1 |= (c2 >> 6) & 0x03; | |
411 | + rs.Append(base64_code[c1 & 0x3f]); | |
412 | + rs.Append(base64_code[c2 & 0x3f]); | |
413 | + } | |
414 | + | |
415 | + return rs.ToString(); | |
416 | + } | |
417 | + | |
418 | + /// <summary>Look up the 3 bits base64-encoded by the specified | |
419 | + /// character, range-checking against the conversion | |
420 | + /// table.</summary> | |
421 | + /// <param name="c">The Base64-encoded value</param> | |
422 | + /// <returns>The decoded value of <c>x</c></returns> | |
423 | + private static int Char64(char c) { | |
424 | + int i = (int)c; | |
425 | + return (i < 0 || i > index_64.Length) ? -1 : index_64[i]; | |
426 | + } | |
427 | + | |
428 | + /// <summary>Decode a string encoded using BCrypt's Base64 scheme to a | |
429 | + /// byte array. Note that this is _not_ compatible with the standard | |
430 | + /// MIME-Base64 encoding.</summary> | |
431 | + /// <param name="s">The string to decode</param> | |
432 | + /// <param name="maximumLength">The maximum number of bytes to decode</param> | |
433 | + /// <returns>An array containing the decoded bytes</returns> | |
434 | + private static byte[] DecodeBase64(string s, int maximumLength) { | |
435 | + | |
436 | + List<byte> bytes = new List<byte>(Math.Min(maximumLength, s.Length)); | |
437 | + | |
438 | + if (maximumLength <= 0) { | |
439 | + throw new ArgumentOutOfRangeException("maximumLength", maximumLength, null); | |
440 | + } | |
441 | + | |
442 | + for (int offset = 0, slen = s.Length, length = 0; offset < slen - 1 && length < maximumLength; ) { | |
443 | + int c1 = Char64(s[offset++]); | |
444 | + int c2 = Char64(s[offset++]); | |
445 | + if (c1 == -1 || c2 == -1) { | |
446 | + break; | |
447 | + } | |
448 | + | |
449 | + bytes.Add((byte)((c1 << 2) | ((c2 & 0x30) >> 4))); | |
450 | + if (++length >= maximumLength || offset >= s.Length) { | |
451 | + break; | |
452 | + } | |
453 | + | |
454 | + int c3 = Char64(s[offset++]); | |
455 | + if (c3 == -1) { | |
456 | + break; | |
457 | + } | |
458 | + | |
459 | + bytes.Add((byte)(((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2))); | |
460 | + if (++length >= maximumLength || offset >= s.Length) { | |
461 | + break; | |
462 | + } | |
463 | + | |
464 | + int c4 = Char64(s[offset++]); | |
465 | + bytes.Add((byte)(((c3 & 0x03) << 6) | c4)); | |
466 | + | |
467 | + ++length; | |
468 | + } | |
469 | + | |
470 | + return bytes.ToArray(); | |
471 | + } | |
472 | + | |
473 | + /// <summary> | |
474 | + /// Blowfish encipher a single 64-bit block encoded as two 32-bit | |
475 | + /// halves. | |
476 | + /// </summary> | |
477 | + /// <param name="block">An array containing the two 32-bit half | |
478 | + /// blocks.</param> | |
479 | + /// <param name="offset">The position in the array of the | |
480 | + /// blocks.</param> | |
481 | + private void Encipher(uint[] block, int offset) { | |
482 | + | |
483 | + uint i, n, l = block[offset], r = block[offset + 1]; | |
484 | + | |
485 | + l ^= this.p[0]; | |
486 | + for (i = 0; i <= BLOWFISH_NUM_ROUNDS - 2;) { | |
487 | + // Feistel substitution on left word | |
488 | + n = this.s[(l >> 24) & 0xff]; | |
489 | + n += this.s[0x100 | ((l >> 16) & 0xff)]; | |
490 | + n ^= this.s[0x200 | ((l >> 8) & 0xff)]; | |
491 | + n += this.s[0x300 | (l & 0xff)]; | |
492 | + r ^= n ^ this.p[++i]; | |
493 | + | |
494 | + // Feistel substitution on right word | |
495 | + n = this.s[(r >> 24) & 0xff]; | |
496 | + n += this.s[0x100 | ((r >> 16) & 0xff)]; | |
497 | + n ^= this.s[0x200 | ((r >> 8) & 0xff)]; | |
498 | + n += this.s[0x300 | (r & 0xff)]; | |
499 | + l ^= n ^ this.p[++i]; | |
500 | + } | |
501 | + block[offset] = r ^ this.p[BLOWFISH_NUM_ROUNDS + 1]; | |
502 | + block[offset + 1] = l; | |
503 | + } | |
504 | + | |
505 | + /// <summary> | |
506 | + /// Cycically extract a word of key material. | |
507 | + /// </summary> | |
508 | + /// <param name="data">The string to extract the data | |
509 | + /// from.</param> | |
510 | + /// <param name="offset">The current offset into data.</param> | |
511 | + /// <returns>The next work of material from data.</returns> | |
512 | + private static uint StreamToWord(byte[] data, ref int offset) { | |
513 | + | |
514 | + uint word = 0; | |
515 | + | |
516 | + for (int i = 0; i < 4; i++) { | |
517 | + word = (word << 8) | data[offset]; | |
518 | + offset = (offset + 1) % data.Length; | |
519 | + } | |
520 | + | |
521 | + return word; | |
522 | + } | |
523 | + | |
524 | + /// <summary> | |
525 | + /// Initialize the Blowfish key schedule. | |
526 | + /// </summary> | |
527 | + private void InitKey() { | |
528 | + this.p = new uint[p_orig.Length]; | |
529 | + p_orig.CopyTo(this.p, 0); | |
530 | + this.s = new uint[s_orig.Length]; | |
531 | + s_orig.CopyTo(this.s, 0); | |
532 | + } | |
533 | + | |
534 | + /// <summary> | |
535 | + /// Key the Blowfish cipher. | |
536 | + /// </summary> | |
537 | + /// <param name="key">An array containing the key.</param> | |
538 | + private void Key(byte[] key) { | |
539 | + | |
540 | + uint[] lr = { 0, 0 }; | |
541 | + int plen = this.p.Length, slen = this.s.Length; | |
542 | + | |
543 | + int offset = 0; | |
544 | + for (int i = 0; i < plen; i++) { | |
545 | + this.p[i] = this.p[i] ^ StreamToWord(key, ref offset); | |
546 | + } | |
547 | + | |
548 | + for (int i = 0; i < plen; i += 2) { | |
549 | + Encipher(lr, 0); | |
550 | + this.p[i] = lr[0]; | |
551 | + this.p[i + 1] = lr[1]; | |
552 | + } | |
553 | + | |
554 | + for (int i = 0; i < slen; i += 2) { | |
555 | + Encipher(lr, 0); | |
556 | + this.s[i] = lr[0]; | |
557 | + this.s[i + 1] = lr[1]; | |
558 | + } | |
559 | + } | |
560 | + | |
561 | + /// <summary> | |
562 | + /// Perform the "enhanced key schedule" step described by Provos | |
563 | + /// and Mazieres in "A Future-Adaptable Password Scheme" | |
564 | + /// (http://www.openbsd.org/papers/bcrypt-paper.ps). | |
565 | + /// </summary> | |
566 | + /// <param name="data">Salt information.</param> | |
567 | + /// <param name="key">Password information.</param> | |
568 | + private void EksKey(byte[] data, byte[] key) { | |
569 | + | |
570 | + uint[] lr = { 0, 0 }; | |
571 | + int plen = this.p.Length, slen = this.s.Length; | |
572 | + | |
573 | + int keyOffset = 0; | |
574 | + for (int i = 0; i < plen; i++) { | |
575 | + this.p[i] = this.p[i] ^ StreamToWord(key, ref keyOffset); | |
576 | + } | |
577 | + | |
578 | + int dataOffset = 0; | |
579 | + for (int i = 0; i < plen; i += 2) { | |
580 | + lr[0] ^= StreamToWord(data, ref dataOffset); | |
581 | + lr[1] ^= StreamToWord(data, ref dataOffset); | |
582 | + Encipher(lr, 0); | |
583 | + this.p[i] = lr[0]; | |
584 | + this.p[i + 1] = lr[1]; | |
585 | + } | |
586 | + | |
587 | + for (int i = 0; i < slen; i += 2) { | |
588 | + lr[0] ^= StreamToWord(data, ref dataOffset); | |
589 | + lr[1] ^= StreamToWord(data, ref dataOffset); | |
590 | + Encipher(lr, 0); | |
591 | + this.s[i] = lr[0]; | |
592 | + this.s[i + 1] = lr[1]; | |
593 | + } | |
594 | + } | |
595 | + | |
596 | + /// <summary> | |
597 | + /// Perform the central password hashing step in the bcrypt | |
598 | + /// scheme. | |
599 | + /// </summary> | |
600 | + /// <param name="password">The password to hash.</param> | |
601 | + /// <param name="salt">The binary salt to hash with the | |
602 | + /// password.</param> | |
603 | + /// <param name="logRounds">The binary logarithm of the number of | |
604 | + /// rounds of hashing to apply.</param> | |
605 | + /// <returns>An array containing the binary hashed password.</returns> | |
606 | + private byte[] CryptRaw(byte[] password, byte[] salt, int logRounds) { | |
607 | + | |
608 | + uint[] cdata = new uint[bf_crypt_ciphertext.Length]; | |
609 | + bf_crypt_ciphertext.CopyTo(cdata, 0); | |
610 | + | |
611 | + int clen = cdata.Length; | |
612 | + byte[] ret; | |
613 | + | |
614 | + if (logRounds < 4 || logRounds > 31) { | |
615 | + throw new ArgumentOutOfRangeException("logRounds", logRounds, null); | |
616 | + } | |
617 | + | |
618 | + int rounds = 1 << logRounds; | |
619 | + if (salt.Length != BCRYPT_SALT_LEN) { | |
620 | + throw new ArgumentException("Invalid salt length.", "salt"); | |
621 | + } | |
622 | + | |
623 | + InitKey(); | |
624 | + EksKey(salt, password); | |
625 | + | |
626 | + for (int i = 0; i < rounds; i++) { | |
627 | + Key(password); | |
628 | + Key(salt); | |
629 | + } | |
630 | + | |
631 | + for (int i = 0; i < 64; i++) { | |
632 | + for (int j = 0; j < (clen >> 1); j++) { | |
633 | + Encipher(cdata, j << 1); | |
634 | + } | |
635 | + } | |
636 | + | |
637 | + ret = new byte[clen * 4]; | |
638 | + for (int i = 0, j = 0; i < clen; i++) { | |
639 | + ret[j++] = (byte)((cdata[i] >> 24) & 0xff); | |
640 | + ret[j++] = (byte)((cdata[i] >> 16) & 0xff); | |
641 | + ret[j++] = (byte)((cdata[i] >> 8) & 0xff); | |
642 | + ret[j++] = (byte)(cdata[i] & 0xff); | |
643 | + } | |
644 | + | |
645 | + return ret; | |
646 | + } | |
647 | + | |
648 | + /// <summary> | |
649 | + /// Hash a password using the OpenBSD bcrypt scheme. | |
650 | + /// </summary> | |
651 | + /// <param name="password">The password to hash.</param> | |
652 | + /// <param name="salt">The salt to hash with (perhaps generated | |
653 | + /// using <c>BCrypt.GenerateSalt</c>).</param> | |
654 | + /// <returns>The hashed password.</returns> | |
655 | + public static string HashPassword(string password, string salt) { | |
656 | + if (password == null) { | |
657 | + throw new ArgumentNullException("password"); | |
658 | + } | |
659 | + if (salt == null) { | |
660 | + throw new ArgumentNullException("salt"); | |
661 | + } | |
662 | + | |
663 | + char minor = (char)0; | |
664 | + | |
665 | + if (salt[0] != '$' || salt[1] != '2') { | |
666 | + throw new ArgumentException("Invalid salt version"); | |
667 | + } | |
668 | + | |
669 | + int offset; | |
670 | + if (salt[1] != '$') { | |
671 | + minor = salt[2]; | |
672 | + if (minor != 'a' || salt[3] != '$') { | |
673 | + throw new ArgumentException("Invalid salt revision"); | |
674 | + } | |
675 | + offset = 4; | |
676 | + } else { | |
677 | + offset = 3; | |
678 | + } | |
679 | + | |
680 | + // Extract number of rounds | |
681 | + if (salt[offset + 2] > '$') { | |
682 | + throw new ArgumentException("Missing salt rounds"); | |
683 | + } | |
684 | + | |
685 | + int rounds = Int32.Parse(salt.Substring(offset, 2), NumberFormatInfo.InvariantInfo); | |
686 | + | |
687 | + byte[] passwordBytes = Encoding.UTF8.GetBytes(password + (minor >= 'a' ? "\0" : String.Empty)); | |
688 | + byte[] saltBytes = DecodeBase64(salt.Substring(offset + 3, 22), | |
689 | + BCRYPT_SALT_LEN); | |
690 | + | |
691 | + BCrypt bcrypt = new BCrypt(); | |
692 | + | |
693 | + byte[] hashed = bcrypt.CryptRaw(passwordBytes, saltBytes, rounds); | |
694 | + | |
695 | + StringBuilder rs = new StringBuilder(); | |
696 | + | |
697 | + rs.Append("$2"); | |
698 | + if (minor >= 'a') { | |
699 | + rs.Append(minor); | |
700 | + } | |
701 | + rs.Append('$'); | |
702 | + if (rounds < 10) { | |
703 | + rs.Append('0'); | |
704 | + } | |
705 | + rs.Append(rounds); | |
706 | + rs.Append('$'); | |
707 | + rs.Append(EncodeBase64(saltBytes, saltBytes.Length)); | |
708 | + rs.Append(EncodeBase64(hashed, | |
709 | + (bf_crypt_ciphertext.Length * 4) - 1)); | |
710 | + | |
711 | + return rs.ToString(); | |
712 | + } | |
713 | + | |
714 | + /// <summary> | |
715 | + /// Generate a salt for use with the BCrypt.HashPassword() method. | |
716 | + /// </summary> | |
717 | + /// <param name="logRounds">The log2 of the number of rounds of | |
718 | + /// hashing to apply. The work factor therefore increases as (2 ** | |
719 | + /// logRounds).</param> | |
720 | + /// <returns>An encoded salt value.</returns> | |
721 | + public static string GenerateSalt(int logRounds) { | |
722 | + | |
723 | + byte[] randomBytes = new byte[BCRYPT_SALT_LEN]; | |
724 | + | |
725 | + RandomNumberGenerator.Create().GetBytes(randomBytes); | |
726 | + | |
727 | + StringBuilder rs = new StringBuilder((randomBytes.Length * 2) + 8); | |
728 | + | |
729 | + rs.Append("$2a$"); | |
730 | + if (logRounds < 10) { | |
731 | + rs.Append('0'); | |
732 | + } | |
733 | + rs.Append(logRounds); | |
734 | + rs.Append('$'); | |
735 | + rs.Append(EncodeBase64(randomBytes, randomBytes.Length)); | |
736 | + | |
737 | + return rs.ToString(); | |
738 | + } | |
739 | + | |
740 | + /// <summary> | |
741 | + /// Generate a salt for use with the <c>BCrypt.HashPassword()</c> | |
742 | + /// method, selecting a reasonable default for the number of hashing | |
743 | + /// rounds to apply. | |
744 | + /// </summary> | |
745 | + /// <returns>An encoded salt value.</returns> | |
746 | + public static string GenerateSalt() { | |
747 | + return GenerateSalt(GENSALT_DEFAULT_LOG2_ROUNDS); | |
748 | + } | |
749 | + | |
750 | + /// <summary> | |
751 | + /// Check that a plaintext password matches a previously hashed | |
752 | + /// one. | |
753 | + /// </summary> | |
754 | + /// <param name="plaintext">The plaintext password to verify.</param> | |
755 | + /// <param name="hashed">The previously hashed password.</param> | |
756 | + /// <returns><c>true</c> if the passwords, <c>false</c> | |
757 | + /// otherwise.</returns> | |
758 | + public static bool CheckPassword(string plaintext, string hashed) { | |
759 | + return StringComparer.Ordinal.Compare(hashed, HashPassword(plaintext, hashed)) == 0; | |
760 | + } | |
761 | + | |
762 | +} | |
763 | + | |
764 | --- chronojump-1.7.0/chronojump_server.orig/chronojumpServerCSharp.cs 1970-01-01 01:00:00.000000000 +0100 | |
765 | +++ chronojump-1.7.0/chronojump_server/chronojumpServerCSharp.cs 2017-02-12 08:52:06.453740036 +0100 | |
766 | @@ -0,0 +1,565 @@ | |
767 | +/* | |
768 | + * This file is part of ChronoJump | |
769 | + * | |
770 | + * Chronojump is free software; you can redistribute it and/or modify | |
771 | + * it under the terms of the GNU General Public License as published by | |
772 | + * the Free Software Foundation; either version 2 of the License, or | |
773 | + * (at your option) any later version. | |
774 | + * | |
775 | + * Chronojump is distributed in the hope that it will be useful, | |
776 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
777 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
778 | + * GNU General Public License for more details. | |
779 | + * | |
780 | + * You should have received a copy of the GNU General Public License | |
781 | + * along with this program; if not, write to the Free Software | |
782 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
783 | + * | |
784 | + * Copyright (C) 2004-2009 Xavier de Blas <xaviblas@gmail.com> | |
785 | + */ | |
786 | + | |
787 | + | |
788 | + | |
789 | + | |
790 | +using System; //for environment | |
791 | +using System.IO; | |
792 | +using System.Web.Services; | |
793 | +//using System.Web; | |
794 | +using System.Web; | |
795 | +using System.Collections; //ArrayList | |
796 | + | |
797 | +using System.Net; //getIP stuff | |
798 | + | |
799 | +//[WebService(Namespace="http://localhost:8080/", //work to connect to corall development from client (from browser works only when online) | |
800 | +//[WebService(Namespace="http://80.32.81.197:8080/", //works to connect with pinux xen from client (from browser don't works) WORKS FROM CLIENT | |
801 | +//[WebService(Namespace="http://server.chronojump.org:8080/", //works to connect with pinux xen from client (from browser don't works) WORKS FROM CLIENT (important: needed the last '/') | |
802 | +//[WebService(Namespace="http://server.chronojump.org/", //works to connect with pinux xen from client (from browser don't works) WORKS FROM CLIENT (important: needed the last '/') | |
803 | +[WebService(Namespace="http://server.chronojump.org:8080/", //2013 server | |
804 | + Description="ChronojumpServer")] | |
805 | +[Serializable] | |
806 | +public class ChronojumpServer { | |
807 | + | |
808 | + [WebMethod(Description="Conecta BBDD")] | |
809 | + public string ConnectDatabase() | |
810 | + { | |
811 | + try { | |
812 | + Sqlite.ConnectServer(); | |
813 | + return "Connected"; | |
814 | + } catch { | |
815 | + return "Unnable to connect"; | |
816 | + } | |
817 | + } | |
818 | + | |
819 | + [WebMethod(Description="Desconecta BBDD")] | |
820 | + public string DisConnectDatabase() | |
821 | + { | |
822 | + try { | |
823 | + Sqlite.DisConnect(); | |
824 | + return "Disconnected"; | |
825 | + } catch { | |
826 | + return "Unnable to disconnect"; | |
827 | + } | |
828 | + } | |
829 | + | |
830 | + [WebMethod(Description="Check actions that client can do depending on it's version)")] | |
831 | + public bool CanINew(string action, string clientVersion) | |
832 | + { | |
833 | + Version cv = new Version(clientVersion); | |
834 | + if(action == Constants.ServerActionUploadSession && cv >= new Version(0,9,3)) | |
835 | + return true; | |
836 | + else if(action == Constants.ServerActionStats && cv >= new Version(0,8,18)) | |
837 | + return true; | |
838 | + else if(action == Constants.ServerActionQuery && cv >= new Version(0,8,18)) | |
839 | + return true; | |
840 | + | |
841 | + return false; | |
842 | + } | |
843 | + | |
844 | +/* note this is old*/ | |
845 | + [WebMethod(Description="Check actions that client can do depending on it's version)")] | |
846 | + public bool CanI(string action, double clientVersion) | |
847 | + { | |
848 | + //comes something like 0.898 | |
849 | + //ONLY used on 0.8.9.7, 0.8.9.8 | |
850 | + Version cv; | |
851 | + if(clientVersion == 0.897) | |
852 | + cv = new Version(0,8,9,7); | |
853 | + else if(clientVersion == 0.898) | |
854 | + cv = new Version(0,8,9,8); | |
855 | + else | |
856 | + return false; //"for if the flyes" | |
857 | + | |
858 | + if(action == Constants.ServerActionUploadSession && cv >= new Version(0,9,3)) | |
859 | + return true; | |
860 | + else if(action == Constants.ServerActionStats && cv >= new Version(0,8)) | |
861 | + return true; | |
862 | + else if(action == Constants.ServerActionQuery && cv >= new Version(0,8,9,6)) | |
863 | + return true; | |
864 | + | |
865 | + return false; | |
866 | + } | |
867 | + | |
868 | + [WebMethod(Description="Query")] | |
869 | + public string Query(string tableName, string test, string variable, | |
870 | + int sex, string ageInterval, | |
871 | + int countryID, int sportID, int speciallityID, int levelID, int evaluatorID) | |
872 | + { | |
873 | + string str = Sqlite.SQLBuildQueryString(tableName, test, variable, | |
874 | + sex, ageInterval, | |
875 | + countryID, sportID, speciallityID, levelID, evaluatorID | |
876 | + ); | |
877 | + | |
878 | + return SqliteServer.Query(str); | |
879 | + } | |
880 | + | |
881 | + | |
882 | + [WebMethod(Description="Stats")] | |
883 | + public string [] Stats() | |
884 | + { | |
885 | + string [] stats = SqliteServer.Stats(); | |
886 | + | |
887 | + return stats; | |
888 | + } | |
889 | + | |
890 | + [WebMethod(Description="Upload a session")] | |
891 | + public int UploadSession(ServerSession mySession) | |
892 | + { | |
893 | + Console.WriteLine(mySession.ToString()); | |
894 | + | |
895 | + int id = mySession.InsertAtDB(false, Constants.SessionTable); | |
896 | + | |
897 | + try { | |
898 | + File.Create("need-to-update-r-graphs"); | |
899 | + } catch { | |
900 | + //file exists and cannot be overwritten | |
901 | + } | |
902 | + | |
903 | + return id; //uniqueID of session at server | |
904 | + } | |
905 | + | |
906 | + [WebMethod(Description="Update session uploadingState")] | |
907 | + public int UpdateSession(int sessionID, int state) | |
908 | + { | |
909 | + SqliteServerSession.UpdateUploadingState(sessionID, state); | |
910 | + | |
911 | + return 1; | |
912 | + } | |
913 | + | |
914 | + [WebMethod(Description="Upload an sport (user defined)")] | |
915 | + public int UploadSport(Sport mySport) | |
916 | + { | |
917 | + int id = -1; | |
918 | + //upload if doesn't exists (uploaded before by this or other evaluator) | |
919 | + if(! Sqlite.Exists(false, Constants.SportTable, mySport.Name)) | |
920 | + id = mySport.InsertAtDB(false); | |
921 | + | |
922 | + return id; //uniqueID of sport at server | |
923 | + } | |
924 | + | |
925 | +/* | |
926 | + [WebMethod(Description="Upload a test type (user defined)")] | |
927 | + //public string UploadTestType(Constants.TestTypes testType, EventType type, int evalSID) | |
928 | + public string UploadTestType(int testType, EventType type, int evalSID) | |
929 | + { | |
930 | + string typeServer = type.Name + "-" + evalSID.ToString(); | |
931 | + | |
932 | + Console.WriteLine("XXXXXXXXXXXXXXXX"); | |
933 | + bool inserted = false; | |
934 | + switch (testType) { | |
935 | + case (int) Constants.TestTypes.JUMP : | |
936 | + JumpType jumpType = (JumpType)type; | |
937 | + Console.WriteLine("JUMP" + typeServer + ":" + jumpType.StartIn + ":" + jumpType.HasWeight + ":" + jumpType.Description); | |
938 | + if(! Sqlite.Exists(false, Constants.JumpTypeTable, typeServer)) { | |
939 | + Console.WriteLine("YYYYYYYYYYYYYYYY"); | |
940 | + //Console.WriteLine("Jump type doesn't exists"); | |
941 | + SqliteJumpType.JumpTypeInsert( | |
942 | + typeServer + ":" + Util.BoolToInt(jumpType.StartIn).ToString() + ":" + | |
943 | + Util.BoolToInt(jumpType.HasWeight).ToString() + ":" + jumpType.Description, | |
944 | + false); | |
945 | + inserted = true; | |
946 | + } | |
947 | + break; | |
948 | + case (int) Constants.TestTypes.JUMP_RJ : | |
949 | + JumpType jumpTypeRj = (JumpType)type; | |
950 | + Console.WriteLine("JUMP_RJ" + typeServer + ":" + jumpTypeRj.Description); | |
951 | + if(! Sqlite.Exists(false, Constants.JumpRjTypeTable, typeServer)) { | |
952 | + //Console.WriteLine("JumpRj type doesn't exists"); | |
953 | + SqliteJumpType.JumpRjTypeInsert( | |
954 | + typeServer + ":" + Util.BoolToInt(jumpTypeRj.StartIn).ToString() + ":" + | |
955 | + Util.BoolToInt(jumpTypeRj.HasWeight).ToString() + ":" + | |
956 | + Util.BoolToInt(jumpTypeRj.JumpsLimited).ToString() + ":" + | |
957 | + jumpTypeRj.FixedValue.ToString() + ":" + | |
958 | + jumpTypeRj.Description, | |
959 | + false); | |
960 | + inserted = true; | |
961 | + } | |
962 | + break; | |
963 | + } | |
964 | + | |
965 | + Console.WriteLine("zzzzzzzzzzzzzzzzzzzzzzzz"); | |
966 | + | |
967 | + if(inserted) | |
968 | + return typeServer; | |
969 | + else | |
970 | + return "-1"; | |
971 | + } | |
972 | + */ | |
973 | + | |
974 | + [WebMethod(Description="Upload a jump type (user defined)")] | |
975 | + public string UploadJumpType(JumpType type, int evalSID) | |
976 | + { | |
977 | + string typeServer = type.Name + "-" + evalSID.ToString(); | |
978 | + | |
979 | + Console.WriteLine("JUMP" + typeServer + ":" + type.StartIn + ":" + type.HasWeight + ":" + type.Description); | |
980 | + if(! Sqlite.Exists(false, Constants.JumpTypeTable, typeServer)) { | |
981 | + SqliteJumpType.JumpTypeInsert( | |
982 | + typeServer + ":" + Util.BoolToInt(type.StartIn).ToString() + ":" + | |
983 | + Util.BoolToInt(type.HasWeight).ToString() + ":" + type.Description, | |
984 | + false); | |
985 | + return typeServer; | |
986 | + } | |
987 | + return "-1"; | |
988 | + } | |
989 | + | |
990 | + [WebMethod(Description="Upload a jumpRj type (user defined)")] | |
991 | + public string UploadJumpRjType(JumpType type, int evalSID) | |
992 | + { | |
993 | + string typeServer = type.Name + "-" + evalSID.ToString(); | |
994 | + | |
995 | + Console.WriteLine("JUMP_RJ" + typeServer + ":" + type.Description); | |
996 | + if(! Sqlite.Exists(false, Constants.JumpRjTypeTable, typeServer)) { | |
997 | + SqliteJumpType.JumpRjTypeInsert( | |
998 | + typeServer + ":" + Util.BoolToInt(type.StartIn).ToString() + ":" + | |
999 | + Util.BoolToInt(type.HasWeight).ToString() + ":" + | |
1000 | + Util.BoolToInt(type.JumpsLimited).ToString() + ":" + | |
1001 | + type.FixedValue.ToString() + ":" + | |
1002 | + type.Description, | |
1003 | + false); | |
1004 | + return typeServer; | |
1005 | + } | |
1006 | + return "-1"; | |
1007 | + } | |
1008 | + | |
1009 | + [WebMethod(Description="Upload a run type (user defined)")] | |
1010 | + public string UploadRunType(RunType type, int evalSID) | |
1011 | + { | |
1012 | + string typeServer = type.Name + "-" + evalSID.ToString(); | |
1013 | + | |
1014 | + Console.WriteLine("RUN" + typeServer + ":" + type.Distance + ":" + type.Description); | |
1015 | + if(! Sqlite.Exists(false, Constants.RunTypeTable, typeServer)) { | |
1016 | + type.Name = typeServer; | |
1017 | + SqliteRunType.Insert(type, Constants.RunTypeTable, false); | |
1018 | + return typeServer; | |
1019 | + } | |
1020 | + return "-1"; | |
1021 | + } | |
1022 | + | |
1023 | + [WebMethod(Description="Upload a run interval type (user defined)")] | |
1024 | + public string UploadRunIntervalType(RunType type, int evalSID) | |
1025 | + { | |
1026 | + string typeServer = type.Name + "-" + evalSID.ToString(); | |
1027 | + | |
1028 | + Console.WriteLine("RUN_I" + typeServer + ":" + type.Distance + ":" + type.Description); | |
1029 | + if(! Sqlite.Exists(false, Constants.RunIntervalTypeTable, typeServer)) { | |
1030 | + type.Name = typeServer; | |
1031 | + SqliteRunIntervalType.Insert(type, Constants.RunIntervalTypeTable, false); | |
1032 | + return typeServer; | |
1033 | + } | |
1034 | + return "-1"; | |
1035 | + } | |
1036 | + | |
1037 | + | |
1038 | + [WebMethod(Description="Upload a person")] | |
1039 | + public int UploadPerson(Person myPerson, int sessionID) | |
1040 | + { | |
1041 | + //store person uniqueID | |
1042 | + int temp = myPerson.UniqueID; | |
1043 | + | |
1044 | + //change value for being inserted with new numeration in server | |
1045 | + myPerson.UniqueID = -1; | |
1046 | + | |
1047 | + //hidden person.Name and comments | |
1048 | + myPerson.Name = ""; | |
1049 | + myPerson.Description = ""; | |
1050 | + | |
1051 | + //do insertion | |
1052 | + int id = myPerson.InsertAtDB(false, Constants.PersonTable); | |
1053 | + | |
1054 | + //roll back person unique id value | |
1055 | + myPerson.UniqueID = temp; | |
1056 | + | |
1057 | + Console.WriteLine("id at server: " + id); | |
1058 | + | |
1059 | + return id; //uniqueID of person at server | |
1060 | + } | |
1061 | + | |
1062 | + [WebMethod(Description="Upload person session if needed")] | |
1063 | + public int UploadPersonSessionIfNeeded(PersonSession ps) | |
1064 | + { | |
1065 | + if(!SqlitePersonSession.PersonSelectExistsInSession(ps.PersonID, ps.SessionID)) { | |
1066 | + Console.WriteLine("personSession needed"); | |
1067 | + Console.WriteLine(ps.ToString()); | |
1068 | + ps.InsertAtDB(false, Constants.PersonSessionTable); | |
1069 | + Console.WriteLine("done"); | |
1070 | + return 1; //unused | |
1071 | + } else | |
1072 | + Console.WriteLine("personSession NOT needed"); | |
1073 | + return 0; //unused | |
1074 | + } | |
1075 | + | |
1076 | + [WebMethod(Description="Upload a ping")] | |
1077 | + public string UploadPing(ServerPing myPing, bool doInsertion) | |
1078 | + { | |
1079 | + //problemes getting user ip: | |
1080 | + //when it works it should be assigned to myPing.IP | |
1081 | + //string a = Request.UserHostName; | |
1082 | + //Console.WriteLine(System.Web.HttpRequest.UserHostAdress); | |
1083 | + | |
1084 | + Console.WriteLine("ping string: " + myPing.ToString()); | |
1085 | + | |
1086 | + | |
1087 | + string strHostName = ""; | |
1088 | + strHostName = System.Net.Dns.GetHostName(); | |
1089 | + IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName); | |
1090 | + IPAddress[] addr = ipEntry.AddressList; | |
1091 | + string ip = addr[addr.Length-1].ToString(); | |
1092 | + | |
1093 | + Console.WriteLine("ip: " + ip); | |
1094 | + | |
1095 | + | |
1096 | + | |
1097 | + | |
1098 | + //!doInsertion is a way to know if server is connected | |
1099 | + //but without inserting nothing | |
1100 | + //is ok before uploading a session | |
1101 | + | |
1102 | + //Console.WriteLine("IP: " + System.Web.HttpRequest.UserHostAddress.ToString()); | |
1103 | + //Console.WriteLine("IP: " + System.Net.HttpRequest.UserHostAddress.ToString()); | |
1104 | + //Console.WriteLine("IP: " + System.Net.Request.UserHostAddress.ToString()); | |
1105 | + //Console.WriteLine("IP: " + HttpContext.Current.Request.UserHostAddress); | |
1106 | + //Console.WriteLine("IP context : " + this.Context.Request.UserHostAddress); | |
1107 | + //Console.WriteLine("IP : " + this.Request.UserHostAddress); | |
1108 | + | |
1109 | + //Console.WriteLine("IP : " + System.Net.HttpListenerRequest.UserHostAddress.ToString()); | |
1110 | + //System.Net.HttpListenerRequest req = new System.Net.HttpListenerRequest(); | |
1111 | + //Console.WriteLine("IP : " + req.UserHostAddress.ToString()); | |
1112 | + | |
1113 | +// System.Net.HttpListenerRequest req; | |
1114 | + //string a = System.Net.HttpListenerRequest.UserHostAddress; | |
1115 | +// string a = req.UserHostAddress; | |
1116 | + | |
1117 | + //string a = this.System.Net.HttpListenerRequest.UserHostAddress; | |
1118 | + | |
1119 | +// System.Net.HttpListenerRequest request = new HttpListenerRequest (String.Empty, "http://localhost/", String.Empty); | |
1120 | +// string a = request.UserHostAddress; | |
1121 | + | |
1122 | + //http://lists.ximian.com/pipermail/mono-list/2007-January/033998.html | |
1123 | + | |
1124 | + /* | |
1125 | + HttpListener listener = new HttpListener(); | |
1126 | + | |
1127 | + listener.AuthenticationSchemeSelectorDelegate += delegate{ | |
1128 | + Console.WriteLine("Asking for authentication scheme"); | |
1129 | + return AuthenticationSchemes.Basic; | |
1130 | + }; | |
1131 | + | |
1132 | + | |
1133 | + | |
1134 | + listener.Start(); | |
1135 | + HttpListenerContext context = listener.GetContext(); | |
1136 | + HttpListenerRequest request = context.Request; | |
1137 | + Console.WriteLine("IP req: " + request.UserHostAddress); | |
1138 | + */ | |
1139 | + | |
1140 | + | |
1141 | + | |
1142 | + if(doInsertion) | |
1143 | + myPing.InsertAtDB(false); | |
1144 | + | |
1145 | + return SqlitePreferences.Select("versionAvailable"); | |
1146 | + } | |
1147 | + | |
1148 | + [WebMethod(Description="Upload an evaluator")] | |
1149 | + public string UploadEvaluator(ServerEvaluator myEval) | |
1150 | + { | |
1151 | + Console.WriteLine("upload. eval string: " + myEval.ToString()); | |
1152 | + | |
1153 | + string idCode; | |
1154 | + Random rnd = new Random(); | |
1155 | + string password = myEval.Name + rnd.Next().ToString(); | |
1156 | + string hashed = BCrypt.HashPassword(password, BCrypt.GenerateSalt(10)); | |
1157 | + | |
1158 | + //insert the password in the server and the hash in the client | |
1159 | + myEval.Code = password; | |
1160 | + | |
1161 | + int id = myEval.InsertAtDB(false); //do insertion | |
1162 | + | |
1163 | + return id.ToString() + ":" + hashed; | |
1164 | + } | |
1165 | + | |
1166 | + [WebMethod(Description="Edit an evaluator")] | |
1167 | + public bool EditEvaluator(ServerEvaluator clientEval, int evalSID) | |
1168 | + { | |
1169 | + Console.WriteLine("edit. eval string: " + clientEval.ToString()); | |
1170 | + | |
1171 | + ServerEvaluator serverEval = SqliteServer.SelectEvaluator(evalSID); | |
1172 | + | |
1173 | + //serveEval.Code is password | |
1174 | + //clientEval.Code is hash | |
1175 | + bool matches = BCrypt.CheckPassword(serverEval.Code, clientEval.Code); | |
1176 | + if(matches) { | |
1177 | + //put the uniqueID that corresponds in server | |
1178 | + clientEval.UniqueID = evalSID; | |
1179 | + | |
1180 | + //put the pass code instead of the client password hash | |
1181 | + clientEval.Code = serverEval.Code; | |
1182 | + | |
1183 | + clientEval.Update(false); //do update | |
1184 | + return true; | |
1185 | + } | |
1186 | + | |
1187 | + return false; | |
1188 | + } | |
1189 | + | |
1190 | + [WebMethod(Description="Select evaluators")] | |
1191 | + public string [] SelectEvaluators(bool addAnyString) | |
1192 | + { | |
1193 | + Console.WriteLine("select evaluators"); | |
1194 | + | |
1195 | + return SqliteServer.SelectEvaluators(addAnyString); | |
1196 | + } | |
1197 | + | |
1198 | + | |
1199 | +/* | |
1200 | + [WebMethod(Description="Upload a test")] | |
1201 | + //public int UploadTest (Event myTest, Constants.TestTypes type, string tableName) | |
1202 | + public int UploadTest (Event myTest, int type, string tableName) | |
1203 | + { | |
1204 | + //store event uniqueID | |
1205 | + int temp = myTest.UniqueID; | |
1206 | + | |
1207 | + //change value for being inserted with new numeration in server | |
1208 | + myTest.UniqueID = -1; | |
1209 | + | |
1210 | + //insert | |
1211 | + int id = 0; | |
1212 | + switch (type) { | |
1213 | + case (int) Constants.TestTypes.JUMP : | |
1214 | + Jump jump = (Jump)myTest; | |
1215 | + id = jump.InsertAtDB(false, tableName); | |
1216 | + break; | |
1217 | + case (int) Constants.TestTypes.JUMP_RJ : | |
1218 | + JumpRj jumpRj = (JumpRj)myTest; | |
1219 | + id = jumpRj.InsertAtDB(false, tableName); | |
1220 | + break; | |
1221 | + case (int) Constants.TestTypes.RUN : | |
1222 | + Run run = (Run)myTest; | |
1223 | + id = run.InsertAtDB(false, tableName); | |
1224 | + break; | |
1225 | + case (int) Constants.TestTypes.RUN_I : | |
1226 | + RunInterval runI = (RunInterval)myTest; | |
1227 | + id = runI.InsertAtDB(false, tableName); | |
1228 | + break; | |
1229 | + case (int) Constants.TestTypes.RT : | |
1230 | + ReactionTime rt = (ReactionTime)myTest; | |
1231 | + id = rt.InsertAtDB(false, tableName); | |
1232 | + break; | |
1233 | + case (int) Constants.TestTypes.PULSE : | |
1234 | + Pulse pulse = (Pulse)myTest; | |
1235 | + id = pulse.InsertAtDB(false, tableName); | |
1236 | + break; | |
1237 | + } | |
1238 | + | |
1239 | + //roll back person unique id value | |
1240 | + myTest.UniqueID = temp; | |
1241 | + | |
1242 | + return id; | |
1243 | + } | |
1244 | + */ | |
1245 | + | |
1246 | + [WebMethod(Description="Upload a jump")] | |
1247 | + public int UploadJump (Jump myTest) | |
1248 | + { | |
1249 | + //store event uniqueID | |
1250 | + int temp = myTest.UniqueID; | |
1251 | + | |
1252 | + //change value for being inserted with new numeration in server | |
1253 | + myTest.UniqueID = -1; | |
1254 | + | |
1255 | + //insert | |
1256 | + int id = myTest.InsertAtDB(false, Constants.JumpTable); | |
1257 | + | |
1258 | + //roll back person unique id value | |
1259 | + myTest.UniqueID = temp; | |
1260 | + | |
1261 | + return id; //uniqueID of person at server | |
1262 | + } | |
1263 | + | |
1264 | + [WebMethod(Description="Upload a jumpRj")] | |
1265 | + public int UploadJumpRj (JumpRj myTest) | |
1266 | + { | |
1267 | + int temp = myTest.UniqueID; | |
1268 | + myTest.UniqueID = -1; | |
1269 | + int id = myTest.InsertAtDB(false, Constants.JumpRjTable); | |
1270 | + myTest.UniqueID = temp; | |
1271 | + return id; //uniqueID of person at server | |
1272 | + } | |
1273 | + | |
1274 | + [WebMethod(Description="Upload a run")] | |
1275 | + public int UploadRun (Run myTest) | |
1276 | + { | |
1277 | + int temp = myTest.UniqueID; | |
1278 | + myTest.UniqueID = -1; | |
1279 | + int id = myTest.InsertAtDB(false, Constants.RunTable); | |
1280 | + myTest.UniqueID = temp; | |
1281 | + return id; //uniqueID of person at server | |
1282 | + } | |
1283 | + | |
1284 | + [WebMethod(Description="Upload a run interval")] | |
1285 | + public int UploadRunI (RunInterval myTest) | |
1286 | + { | |
1287 | + int temp = myTest.UniqueID; | |
1288 | + myTest.UniqueID = -1; | |
1289 | + int id = myTest.InsertAtDB(false, Constants.RunIntervalTable); | |
1290 | + myTest.UniqueID = temp; | |
1291 | + return id; //uniqueID of person at server | |
1292 | + } | |
1293 | + | |
1294 | + [WebMethod(Description="Upload a reaction time")] | |
1295 | + public int UploadRT (ReactionTime myTest) | |
1296 | + { | |
1297 | + int temp = myTest.UniqueID; | |
1298 | + myTest.UniqueID = -1; | |
1299 | + int id = myTest.InsertAtDB(false, Constants.ReactionTimeTable); | |
1300 | + myTest.UniqueID = temp; | |
1301 | + return id; //uniqueID of person at server | |
1302 | + } | |
1303 | + | |
1304 | + [WebMethod(Description="Upload a pulse")] | |
1305 | + public int UploadPulse (Pulse myTest) | |
1306 | + { | |
1307 | + int temp = myTest.UniqueID; | |
1308 | + myTest.UniqueID = -1; | |
1309 | + int id = myTest.InsertAtDB(false, Constants.PulseTable); | |
1310 | + myTest.UniqueID = temp; | |
1311 | + return id; //uniqueID of person at server | |
1312 | + } | |
1313 | + | |
1314 | + [WebMethod(Description="Upload a multiChronopic")] | |
1315 | + public int UploadMultiChronopic (MultiChronopic myTest) | |
1316 | + { | |
1317 | + int temp = myTest.UniqueID; | |
1318 | + myTest.UniqueID = -1; | |
1319 | + int id = myTest.InsertAtDB(false, Constants.MultiChronopicTable); | |
1320 | + myTest.UniqueID = temp; | |
1321 | + return id; //uniqueID of person at server | |
1322 | + } | |
1323 | + | |
1324 | + | |
1325 | + [WebMethod(Description="List directory files (only as a sample)")] | |
1326 | + public string [] ListDirectory(string path) { | |
1327 | + return Directory.GetFileSystemEntries(path); | |
1328 | + } | |
1329 | + | |
1330 | + | |
1331 | +} | |
1332 | --- chronojump-1.7.0/chronojump_server.orig/Makefile.am 1970-01-01 01:00:00.000000000 +0100 | |
1333 | +++ chronojump-1.7.0/chronojump_server/Makefile.am 2017-02-12 08:49:57.753741504 +0100 | |
1334 | @@ -0,0 +1,42 @@ | |
1335 | +ASSEMBLY = chronojumpServer | |
1336 | +TARGET = library | |
1337 | +LINK = $(REF_DEP_CHRONOJUMP_SERVER) | |
1338 | + | |
1339 | +SOURCES = \ | |
1340 | + chronojumpServerCSharp.cs\ | |
1341 | + BCrypt.cs\ | |
1342 | + ../src/sqlite/*.cs\ | |
1343 | + ../src/util.cs\ | |
1344 | + ../src/utilAll.cs\ | |
1345 | + ../src/utilDate.cs\ | |
1346 | + ../src/encoder.cs\ | |
1347 | + ../src/executeAuto.cs\ | |
1348 | + ../src/event.cs\ | |
1349 | + ../src/genericObjects.cs\ | |
1350 | + ../src/jump.cs\ | |
1351 | + ../src/run.cs\ | |
1352 | + ../src/person.cs\ | |
1353 | + ../src/personSession.cs\ | |
1354 | + ../src/preferences.cs\ | |
1355 | + ../src/pulse.cs\ | |
1356 | + ../src/reactionTime.cs\ | |
1357 | + ../src/session.cs\ | |
1358 | + ../src/eventType.cs\ | |
1359 | + ../src/jumpType.cs\ | |
1360 | + ../src/runType.cs\ | |
1361 | + ../src/pulseType.cs\ | |
1362 | + ../src/constants.cs\ | |
1363 | + ../src/multiChronopic.cs\ | |
1364 | + ../src/sport.cs\ | |
1365 | + ../src/log.cs\ | |
1366 | + ../src/logB.cs \ | |
1367 | + ../src/logConsoleCrayon.cs \ | |
1368 | + ../src/serverPing.cs\ | |
1369 | + ../src/serverEvaluator.cs\ | |
1370 | + ../src/oldCodeNeedToDBConvert/person.cs\ | |
1371 | + ../src/oldCodeNeedToDBConvert/personSession.cs\ | |
1372 | + ../src/oldCodeNeedToDBConvert/sqlite/*.cs | |
1373 | + | |
1374 | + | |
1375 | +include $(top_srcdir)/build/build.mk | |
1376 | + | |
1377 | --- chronojump-1.7.0/Makefile.am.orig 2017-02-12 08:57:32.753736311 +0100 | |
1378 | +++ chronojump-1.7.0/Makefile.am 2017-02-12 08:57:36.483736266 +0100 | |
1379 | @@ -10,6 +10,8 @@ | |
1380 | SUBDIRS += chronopic-tests #chronojump_server (disabled) | |
1381 | endif | |
1382 | ||
1383 | +DIST_SUBDIRS = build chronopic-firmware encoder libcesarplayer CesarPlayer src po manual chronopic-tests chronojump_server | |
1384 | + | |
1385 | if OSTYPE_WINDOWS | |
1386 | win32-installer: all install | |
1387 | make -C win32 installer | |
91d08a1d JB |
1388 | --- chronojump-1.7.1/rfid.orig/chronojump_rfid_capture.py 1970-01-01 01:00:00.000000000 +0100 |
1389 | +++ chronojump-1.7.1/rfid/chronojump_rfid_capture.py 2017-07-29 17:56:43.304012163 +0200 | |
1390 | @@ -0,0 +1,65 @@ | |
1391 | +#!/usr/bin/env python | |
1392 | +# -*- coding: utf8 -*- | |
1393 | + | |
1394 | +import RPi.GPIO as GPIO | |
1395 | +import MFRC522 | |
1396 | +import signal | |
1397 | + | |
1398 | +continue_reading = True | |
1399 | + | |
1400 | +# Capture SIGINT for cleanup when the script is aborted | |
1401 | +def end_read(signal,frame): | |
1402 | + global continue_reading | |
1403 | + print "Ctrl+C captured, ending read." | |
1404 | + continue_reading = False | |
1405 | + GPIO.cleanup() | |
1406 | + | |
1407 | +# Hook the SIGINT | |
1408 | +signal.signal(signal.SIGINT, end_read) | |
1409 | + | |
1410 | +# Create an object of the class MFRC522 | |
1411 | +MIFAREReader = MFRC522.MFRC522() | |
1412 | + | |
1413 | +# Welcome message | |
1414 | +print "Welcome to the MFRC522 data read example" | |
1415 | +print "Press Ctrl-C to stop." | |
1416 | + | |
1417 | +# This loop keeps checking for chips. If one is near it will get the UID and authenticate | |
1418 | +while continue_reading: | |
1419 | + | |
1420 | + # Scan for cards | |
1421 | + (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL) | |
1422 | + | |
1423 | + # If a card is found | |
1424 | + if status == MIFAREReader.MI_OK: | |
1425 | + print "Card detected" | |
1426 | + | |
1427 | + # Get the UID of the card | |
1428 | + (status,uid) = MIFAREReader.MFRC522_Anticoll() | |
1429 | + | |
1430 | + # If we have the UID, continue | |
1431 | + if status == MIFAREReader.MI_OK: | |
1432 | + | |
1433 | + # Print UID | |
1434 | + print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]) | |
1435 | + | |
1436 | + file = open("/tmp/chronojump_rfid.txt", "w") | |
1437 | + file.write(str(uid[0]) + "," + str(uid[1]) + "," + str(uid[2]) + "," + str(uid[3])) | |
1438 | + file.close() | |
1439 | + | |
1440 | + # This is the default key for authentication | |
1441 | + key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF] | |
1442 | + | |
1443 | + # Select the scanned tag | |
1444 | + MIFAREReader.MFRC522_SelectTag(uid) | |
1445 | + | |
1446 | + # Authenticate | |
1447 | + status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid) | |
1448 | + | |
1449 | + # Check if authenticated | |
1450 | + if status == MIFAREReader.MI_OK: | |
1451 | + MIFAREReader.MFRC522_Read(8) | |
1452 | + MIFAREReader.MFRC522_StopCrypto1() | |
1453 | + else: | |
1454 | + print "Authentication error" | |
1455 | + | |
1456 | --- chronojump-1.7.1/rfid.orig/MFRC522.py 1970-01-01 01:00:00.000000000 +0100 | |
1457 | +++ chronojump-1.7.1/rfid/MFRC522.py 2017-07-29 17:56:35.870678915 +0200 | |
1458 | @@ -0,0 +1,397 @@ | |
1459 | +#!/usr/bin/env python\r | |
1460 | +# -*- coding: utf8 -*-\r | |
1461 | +\r | |
1462 | +import RPi.GPIO as GPIO\r | |
1463 | +import spi\r | |
1464 | +import signal\r | |
1465 | +import time\r | |
1466 | + \r | |
1467 | +class MFRC522:\r | |
1468 | + NRSTPD = 22\r | |
1469 | + \r | |
1470 | + MAX_LEN = 16\r | |
1471 | + \r | |
1472 | + PCD_IDLE = 0x00\r | |
1473 | + PCD_AUTHENT = 0x0E\r | |
1474 | + PCD_RECEIVE = 0x08\r | |
1475 | + PCD_TRANSMIT = 0x04\r | |
1476 | + PCD_TRANSCEIVE = 0x0C\r | |
1477 | + PCD_RESETPHASE = 0x0F\r | |
1478 | + PCD_CALCCRC = 0x03\r | |
1479 | + \r | |
1480 | + PICC_REQIDL = 0x26\r | |
1481 | + PICC_REQALL = 0x52\r | |
1482 | + PICC_ANTICOLL = 0x93\r | |
1483 | + PICC_SElECTTAG = 0x93\r | |
1484 | + PICC_AUTHENT1A = 0x60\r | |
1485 | + PICC_AUTHENT1B = 0x61\r | |
1486 | + PICC_READ = 0x30\r | |
1487 | + PICC_WRITE = 0xA0\r | |
1488 | + PICC_DECREMENT = 0xC0\r | |
1489 | + PICC_INCREMENT = 0xC1\r | |
1490 | + PICC_RESTORE = 0xC2\r | |
1491 | + PICC_TRANSFER = 0xB0\r | |
1492 | + PICC_HALT = 0x50\r | |
1493 | + \r | |
1494 | + MI_OK = 0\r | |
1495 | + MI_NOTAGERR = 1\r | |
1496 | + MI_ERR = 2\r | |
1497 | + \r | |
1498 | + Reserved00 = 0x00\r | |
1499 | + CommandReg = 0x01\r | |
1500 | + CommIEnReg = 0x02\r | |
1501 | + DivlEnReg = 0x03\r | |
1502 | + CommIrqReg = 0x04\r | |
1503 | + DivIrqReg = 0x05\r | |
1504 | + ErrorReg = 0x06\r | |
1505 | + Status1Reg = 0x07\r | |
1506 | + Status2Reg = 0x08\r | |
1507 | + FIFODataReg = 0x09\r | |
1508 | + FIFOLevelReg = 0x0A\r | |
1509 | + WaterLevelReg = 0x0B\r | |
1510 | + ControlReg = 0x0C\r | |
1511 | + BitFramingReg = 0x0D\r | |
1512 | + CollReg = 0x0E\r | |
1513 | + Reserved01 = 0x0F\r | |
1514 | + \r | |
1515 | + Reserved10 = 0x10\r | |
1516 | + ModeReg = 0x11\r | |
1517 | + TxModeReg = 0x12\r | |
1518 | + RxModeReg = 0x13\r | |
1519 | + TxControlReg = 0x14\r | |
1520 | + TxAutoReg = 0x15\r | |
1521 | + TxSelReg = 0x16\r | |
1522 | + RxSelReg = 0x17\r | |
1523 | + RxThresholdReg = 0x18\r | |
1524 | + DemodReg = 0x19\r | |
1525 | + Reserved11 = 0x1A\r | |
1526 | + Reserved12 = 0x1B\r | |
1527 | + MifareReg = 0x1C\r | |
1528 | + Reserved13 = 0x1D\r | |
1529 | + Reserved14 = 0x1E\r | |
1530 | + SerialSpeedReg = 0x1F\r | |
1531 | + \r | |
1532 | + Reserved20 = 0x20 \r | |
1533 | + CRCResultRegM = 0x21\r | |
1534 | + CRCResultRegL = 0x22\r | |
1535 | + Reserved21 = 0x23\r | |
1536 | + ModWidthReg = 0x24\r | |
1537 | + Reserved22 = 0x25\r | |
1538 | + RFCfgReg = 0x26\r | |
1539 | + GsNReg = 0x27\r | |
1540 | + CWGsPReg = 0x28\r | |
1541 | + ModGsPReg = 0x29\r | |
1542 | + TModeReg = 0x2A\r | |
1543 | + TPrescalerReg = 0x2B\r | |
1544 | + TReloadRegH = 0x2C\r | |
1545 | + TReloadRegL = 0x2D\r | |
1546 | + TCounterValueRegH = 0x2E\r | |
1547 | + TCounterValueRegL = 0x2F\r | |
1548 | + \r | |
1549 | + Reserved30 = 0x30\r | |
1550 | + TestSel1Reg = 0x31\r | |
1551 | + TestSel2Reg = 0x32\r | |
1552 | + TestPinEnReg = 0x33\r | |
1553 | + TestPinValueReg = 0x34\r | |
1554 | + TestBusReg = 0x35\r | |
1555 | + AutoTestReg = 0x36\r | |
1556 | + VersionReg = 0x37\r | |
1557 | + AnalogTestReg = 0x38\r | |
1558 | + TestDAC1Reg = 0x39\r | |
1559 | + TestDAC2Reg = 0x3A\r | |
1560 | + TestADCReg = 0x3B\r | |
1561 | + Reserved31 = 0x3C\r | |
1562 | + Reserved32 = 0x3D\r | |
1563 | + Reserved33 = 0x3E\r | |
1564 | + Reserved34 = 0x3F\r | |
1565 | + \r | |
1566 | + serNum = []\r | |
1567 | + \r | |
1568 | + def __init__(self, dev='/dev/spidev0.0', spd=1000000):\r | |
1569 | + spi.openSPI(device=dev,speed=spd)\r | |
1570 | + GPIO.setmode(GPIO.BOARD)\r | |
1571 | + GPIO.setup(22, GPIO.OUT)\r | |
1572 | + GPIO.output(self.NRSTPD, 1)\r | |
1573 | + self.MFRC522_Init()\r | |
1574 | + \r | |
1575 | + def MFRC522_Reset(self):\r | |
1576 | + self.Write_MFRC522(self.CommandReg, self.PCD_RESETPHASE)\r | |
1577 | + \r | |
1578 | + def Write_MFRC522(self, addr, val):\r | |
1579 | + spi.transfer(((addr<<1)&0x7E,val))\r | |
1580 | + \r | |
1581 | + def Read_MFRC522(self, addr):\r | |
1582 | + val = spi.transfer((((addr<<1)&0x7E) | 0x80,0))\r | |
1583 | + return val[1]\r | |
1584 | + \r | |
1585 | + def SetBitMask(self, reg, mask):\r | |
1586 | + tmp = self.Read_MFRC522(reg)\r | |
1587 | + self.Write_MFRC522(reg, tmp | mask)\r | |
1588 | + \r | |
1589 | + def ClearBitMask(self, reg, mask):\r | |
1590 | + tmp = self.Read_MFRC522(reg);\r | |
1591 | + self.Write_MFRC522(reg, tmp & (~mask))\r | |
1592 | + \r | |
1593 | + def AntennaOn(self):\r | |
1594 | + temp = self.Read_MFRC522(self.TxControlReg)\r | |
1595 | + if(~(temp & 0x03)):\r | |
1596 | + self.SetBitMask(self.TxControlReg, 0x03)\r | |
1597 | + \r | |
1598 | + def AntennaOff(self):\r | |
1599 | + self.ClearBitMask(self.TxControlReg, 0x03)\r | |
1600 | + \r | |
1601 | + def MFRC522_ToCard(self,command,sendData):\r | |
1602 | + backData = []\r | |
1603 | + backLen = 0\r | |
1604 | + status = self.MI_ERR\r | |
1605 | + irqEn = 0x00\r | |
1606 | + waitIRq = 0x00\r | |
1607 | + lastBits = None\r | |
1608 | + n = 0\r | |
1609 | + i = 0\r | |
1610 | + \r | |
1611 | + if command == self.PCD_AUTHENT:\r | |
1612 | + irqEn = 0x12\r | |
1613 | + waitIRq = 0x10\r | |
1614 | + if command == self.PCD_TRANSCEIVE:\r | |
1615 | + irqEn = 0x77\r | |
1616 | + waitIRq = 0x30\r | |
1617 | + \r | |
1618 | + self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)\r | |
1619 | + self.ClearBitMask(self.CommIrqReg, 0x80)\r | |
1620 | + self.SetBitMask(self.FIFOLevelReg, 0x80)\r | |
1621 | + \r | |
1622 | + self.Write_MFRC522(self.CommandReg, self.PCD_IDLE); \r | |
1623 | + \r | |
1624 | + while(i<len(sendData)):\r | |
1625 | + self.Write_MFRC522(self.FIFODataReg, sendData[i])\r | |
1626 | + i = i+1\r | |
1627 | + \r | |
1628 | + self.Write_MFRC522(self.CommandReg, command)\r | |
1629 | + \r | |
1630 | + if command == self.PCD_TRANSCEIVE:\r | |
1631 | + self.SetBitMask(self.BitFramingReg, 0x80)\r | |
1632 | + \r | |
1633 | + i = 2000\r | |
1634 | + while True:\r | |
1635 | + n = self.Read_MFRC522(self.CommIrqReg)\r | |
1636 | + i = i - 1\r | |
1637 | + if ~((i!=0) and ~(n&0x01) and ~(n&waitIRq)):\r | |
1638 | + break\r | |
1639 | + \r | |
1640 | + self.ClearBitMask(self.BitFramingReg, 0x80)\r | |
1641 | + \r | |
1642 | + if i != 0:\r | |
1643 | + if (self.Read_MFRC522(self.ErrorReg) & 0x1B)==0x00:\r | |
1644 | + status = self.MI_OK\r | |
1645 | +\r | |
1646 | + if n & irqEn & 0x01:\r | |
1647 | + status = self.MI_NOTAGERR\r | |
1648 | + \r | |
1649 | + if command == self.PCD_TRANSCEIVE:\r | |
1650 | + n = self.Read_MFRC522(self.FIFOLevelReg)\r | |
1651 | + lastBits = self.Read_MFRC522(self.ControlReg) & 0x07\r | |
1652 | + if lastBits != 0:\r | |
1653 | + backLen = (n-1)*8 + lastBits\r | |
1654 | + else:\r | |
1655 | + backLen = n*8\r | |
1656 | + \r | |
1657 | + if n == 0:\r | |
1658 | + n = 1\r | |
1659 | + if n > self.MAX_LEN:\r | |
1660 | + n = self.MAX_LEN\r | |
1661 | + \r | |
1662 | + i = 0\r | |
1663 | + while i<n:\r | |
1664 | + backData.append(self.Read_MFRC522(self.FIFODataReg))\r | |
1665 | + i = i + 1;\r | |
1666 | + else:\r | |
1667 | + status = self.MI_ERR\r | |
1668 | +\r | |
1669 | + return (status,backData,backLen)\r | |
1670 | + \r | |
1671 | + \r | |
1672 | + def MFRC522_Request(self, reqMode):\r | |
1673 | + status = None\r | |
1674 | + backBits = None\r | |
1675 | + TagType = []\r | |
1676 | + \r | |
1677 | + self.Write_MFRC522(self.BitFramingReg, 0x07)\r | |
1678 | + \r | |
1679 | + TagType.append(reqMode);\r | |
1680 | + (status,backData,backBits) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, TagType)\r | |
1681 | + \r | |
1682 | + if ((status != self.MI_OK) | (backBits != 0x10)):\r | |
1683 | + status = self.MI_ERR\r | |
1684 | + \r | |
1685 | + return (status,backBits)\r | |
1686 | + \r | |
1687 | + \r | |
1688 | + def MFRC522_Anticoll(self):\r | |
1689 | + backData = []\r | |
1690 | + serNumCheck = 0\r | |
1691 | + \r | |
1692 | + serNum = []\r | |
1693 | + \r | |
1694 | + self.Write_MFRC522(self.BitFramingReg, 0x00)\r | |
1695 | + \r | |
1696 | + serNum.append(self.PICC_ANTICOLL)\r | |
1697 | + serNum.append(0x20)\r | |
1698 | + \r | |
1699 | + (status,backData,backBits) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE,serNum)\r | |
1700 | + \r | |
1701 | + if(status == self.MI_OK):\r | |
1702 | + i = 0\r | |
1703 | + if len(backData)==5:\r | |
1704 | + while i<4:\r | |
1705 | + serNumCheck = serNumCheck ^ backData[i]\r | |
1706 | + i = i + 1\r | |
1707 | + if serNumCheck != backData[i]:\r | |
1708 | + status = self.MI_ERR\r | |
1709 | + else:\r | |
1710 | + status = self.MI_ERR\r | |
1711 | + \r | |
1712 | + return (status,backData)\r | |
1713 | + \r | |
1714 | + def CalulateCRC(self, pIndata):\r | |
1715 | + self.ClearBitMask(self.DivIrqReg, 0x04)\r | |
1716 | + self.SetBitMask(self.FIFOLevelReg, 0x80);\r | |
1717 | + i = 0\r | |
1718 | + while i<len(pIndata):\r | |
1719 | + self.Write_MFRC522(self.FIFODataReg, pIndata[i])\r | |
1720 | + i = i + 1\r | |
1721 | + self.Write_MFRC522(self.CommandReg, self.PCD_CALCCRC)\r | |
1722 | + i = 0xFF\r | |
1723 | + while True:\r | |
1724 | + n = self.Read_MFRC522(self.DivIrqReg)\r | |
1725 | + i = i - 1\r | |
1726 | + if not ((i != 0) and not (n&0x04)):\r | |
1727 | + break\r | |
1728 | + pOutData = []\r | |
1729 | + pOutData.append(self.Read_MFRC522(self.CRCResultRegL))\r | |
1730 | + pOutData.append(self.Read_MFRC522(self.CRCResultRegM))\r | |
1731 | + return pOutData\r | |
1732 | + \r | |
1733 | + def MFRC522_SelectTag(self, serNum):\r | |
1734 | + backData = []\r | |
1735 | + buf = []\r | |
1736 | + buf.append(self.PICC_SElECTTAG)\r | |
1737 | + buf.append(0x70)\r | |
1738 | + i = 0\r | |
1739 | + while i<5:\r | |
1740 | + buf.append(serNum[i])\r | |
1741 | + i = i + 1\r | |
1742 | + pOut = self.CalulateCRC(buf)\r | |
1743 | + buf.append(pOut[0])\r | |
1744 | + buf.append(pOut[1])\r | |
1745 | + (status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buf)\r | |
1746 | + \r | |
1747 | + if (status == self.MI_OK) and (backLen == 0x18):\r | |
1748 | + print "Size: " + str(backData[0])\r | |
1749 | + return backData[0]\r | |
1750 | + else:\r | |
1751 | + return 0\r | |
1752 | + \r | |
1753 | + def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):\r | |
1754 | + buff = []\r | |
1755 | +\r | |
1756 | + # First byte should be the authMode (A or B)\r | |
1757 | + buff.append(authMode)\r | |
1758 | +\r | |
1759 | + # Second byte is the trailerBlock (usually 7)\r | |
1760 | + buff.append(BlockAddr)\r | |
1761 | +\r | |
1762 | + # Now we need to append the authKey which usually is 6 bytes of 0xFF\r | |
1763 | + i = 0\r | |
1764 | + while(i < len(Sectorkey)):\r | |
1765 | + buff.append(Sectorkey[i])\r | |
1766 | + i = i + 1\r | |
1767 | + i = 0\r | |
1768 | +\r | |
1769 | + # Next we append the first 4 bytes of the UID\r | |
1770 | + while(i < 4):\r | |
1771 | + buff.append(serNum[i])\r | |
1772 | + i = i +1\r | |
1773 | +\r | |
1774 | + # Now we start the authentication itself\r | |
1775 | + (status, backData, backLen) = self.MFRC522_ToCard(self.PCD_AUTHENT,buff)\r | |
1776 | +\r | |
1777 | + # Check if an error occurred\r | |
1778 | + if not(status == self.MI_OK):\r | |
1779 | + print "AUTH ERROR!!"\r | |
1780 | + if not (self.Read_MFRC522(self.Status2Reg) & 0x08) != 0:\r | |
1781 | + print "AUTH ERROR(status2reg & 0x08) != 0"\r | |
1782 | +\r | |
1783 | + # Return the status\r | |
1784 | + return status\r | |
1785 | + \r | |
1786 | + def MFRC522_StopCrypto1(self):\r | |
1787 | + self.ClearBitMask(self.Status2Reg, 0x08)\r | |
1788 | +\r | |
1789 | + def MFRC522_Read(self, blockAddr):\r | |
1790 | + recvData = []\r | |
1791 | + recvData.append(self.PICC_READ)\r | |
1792 | + recvData.append(blockAddr)\r | |
1793 | + pOut = self.CalulateCRC(recvData)\r | |
1794 | + recvData.append(pOut[0])\r | |
1795 | + recvData.append(pOut[1])\r | |
1796 | + (status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, recvData)\r | |
1797 | + if not(status == self.MI_OK):\r | |
1798 | + print "Error while reading!"\r | |
1799 | + i = 0\r | |
1800 | + #xavi: not needed\r | |
1801 | + #if len(backData) == 16:\r | |
1802 | + # print "Sector "+str(blockAddr)+" "+str(backData)\r | |
1803 | + \r | |
1804 | + def MFRC522_Write(self, blockAddr, writeData):\r | |
1805 | + buff = []\r | |
1806 | + buff.append(self.PICC_WRITE)\r | |
1807 | + buff.append(blockAddr)\r | |
1808 | + crc = self.CalulateCRC(buff)\r | |
1809 | + buff.append(crc[0])\r | |
1810 | + buff.append(crc[1])\r | |
1811 | + (status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buff)\r | |
1812 | + if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):\r | |
1813 | + status = self.MI_ERR\r | |
1814 | + \r | |
1815 | + print str(backLen)+" backdata &0x0F == 0x0A "+str(backData[0]&0x0F)\r | |
1816 | + if status == self.MI_OK:\r | |
1817 | + i = 0\r | |
1818 | + buf = []\r | |
1819 | + while i < 16:\r | |
1820 | + buf.append(writeData[i])\r | |
1821 | + i = i + 1\r | |
1822 | + crc = self.CalulateCRC(buf)\r | |
1823 | + buf.append(crc[0])\r | |
1824 | + buf.append(crc[1])\r | |
1825 | + (status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE,buf)\r | |
1826 | + if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):\r | |
1827 | + print "Error while writing"\r | |
1828 | + if status == self.MI_OK:\r | |
1829 | + print "Data written"\r | |
1830 | +\r | |
1831 | + def MFRC522_DumpClassic1K(self, key, uid):\r | |
1832 | + i = 0\r | |
1833 | + while i < 64:\r | |
1834 | + status = self.MFRC522_Auth(self.PICC_AUTHENT1A, i, key, uid)\r | |
1835 | + # Check if authenticated\r | |
1836 | + if status == self.MI_OK:\r | |
1837 | + self.MFRC522_Read(i)\r | |
1838 | + else:\r | |
1839 | + print "Authentication error"\r | |
1840 | + i = i+1\r | |
1841 | +\r | |
1842 | + def MFRC522_Init(self):\r | |
1843 | + GPIO.output(self.NRSTPD, 1)\r | |
1844 | + \r | |
1845 | + self.MFRC522_Reset();\r | |
1846 | + \r | |
1847 | + \r | |
1848 | + self.Write_MFRC522(self.TModeReg, 0x8D)\r | |
1849 | + self.Write_MFRC522(self.TPrescalerReg, 0x3E)\r | |
1850 | + self.Write_MFRC522(self.TReloadRegL, 30)\r | |
1851 | + self.Write_MFRC522(self.TReloadRegH, 0)\r | |
1852 | + \r | |
1853 | + self.Write_MFRC522(self.TxAutoReg, 0x40)\r | |
1854 | + self.Write_MFRC522(self.ModeReg, 0x3D)\r | |
1855 | + self.AntennaOn()\r |