| 205 | function inc(a, b) |
| 206 | { |
| 207 | var carry = 0; |
| 208 | var num = 0; |
| 209 | var i = 0; |
| 210 | |
| 211 | while((carry || (i < a.length) || (i < b.length)) && (i < 100)) |
| 212 | { |
| 213 | num = carry; |
| 214 | if(i < a.length){num += a[i];} |
| 215 | if(i < b.length){num += b[i];} |
| 216 | |
| 217 | if(num >= 256) |
| 218 | { |
| 219 | num -= 256; |
| 220 | carry = 1; |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | carry = 0; |
| 225 | } |
| 226 | |
| 227 | a[i] = num; |
| 228 | |
| 229 | i++; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | function ifposDec(a, b) |
| 234 | { |
| 235 | console.log("GOT " + a + " and " + b); |
| 236 | var carry = 0; |
| 237 | var num = 0; |
| 238 | var i = 0; |
| 239 | |
| 240 | if(b.length > a.length){console.log("RETURNING 0 (1)"); return 0;} |
| 241 | if(b.length == a.length) |
| 242 | { |
| 243 | i = a.length - 1; |
| 244 | while(i >= 0) |
| 245 | { |
| 246 | if(a[i] > b[i]){console.log("BREAKING"); break;} |
| 247 | if(a[i] < b[i]){console.log("RETURNING 0 (2)"); return 0;} |
| 248 | i--; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | i = 0; |
| 253 | while((i < a.length) || (i < b.length)) |
| 254 | { |
| 255 | num = -carry; |
| 256 | if(i < a.length){num += a[i];} |
| 257 | if(i < b.length){num -= b[i];} |
| 258 | |
| 259 | if(num < 0) |
| 260 | { |
| 261 | num += 256; |
| 262 | carry = 1; |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | carry = 0; |
| 267 | } |
| 268 | |
| 269 | a[i] = num; |
| 270 | i++ |
| 271 | } |
| 272 | console.log("NUM is " + num); |
| 273 | |
| 274 | return 1; |
| 275 | } |
| 276 | |
| 277 | function convertNum(a) |
| 278 | { |
| 279 | var result = new Array(); |
| 280 | var i; |
| 281 | var convert = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]; |
| 282 | |
| 283 | if(a.length == 0) |
| 284 | { |
| 285 | result.push("0"); |
| 286 | return result; |
| 287 | } |
| 288 | |
| 289 | for(i = a.length - 1; i >= 0; i--) |
| 290 | { |
| 291 | console.log("i is " + i + " a[i] is " + a[i] + " a[i]/16 is " + Math.floor(a[i]/16) + " a[i]%16 is " + Math.floor(a[i]%16)); |
| 292 | console.log("Adding " + convert[Math.floor(a[i]/16)] + " and " + convert[Math.floor(a[i]%16)]); |
| 293 | result.push(convert[Math.floor(a[i]/16)]); |
| 294 | result.push(convert[Math.floor(a[i]%16)]); |
| 295 | } |
| 296 | |
| 297 | var resultString = ""; |
| 298 | for(var j = 0; j < result.length; j++) |
| 299 | { |
| 300 | resultString += result[j]; |
| 301 | } |
| 302 | |
| 303 | return resultString; |
| 304 | } |
| 305 | |
| 306 | gs.functions.hashString = function(str) |
| 307 | { |
| 308 | var remainder = new Array(); |
| 309 | var primePow = new Array(); |
| 310 | var pow = |
| 311 | [ |
| 312 | 255, 255, 255, |
| 313 | 255, 255, 255, |
| 314 | 255, 255, 255, |
| 315 | 255, 255, 1 |
| 316 | ]; |
| 317 | |
| 318 | for(var i = 0; i < 8; i++) |
| 319 | { |
| 320 | primePow.push(pow); |
| 321 | inc(pow, pow); |
| 322 | } |
| 323 | |
| 324 | for(var i = 0; i < str.length; i++) |
| 325 | { |
| 326 | var c = str.charAt(i); |
| 327 | |
| 328 | if(remainder.length == 99) |
| 329 | { |
| 330 | return null; |
| 331 | } |
| 332 | |
| 333 | for(var j = remainder.length; j > 0; j--) |
| 334 | { |
| 335 | remainder[i] = remainder[i-1]; |
| 336 | } |
| 337 | remainder[0] = c; |
| 338 | |
| 339 | for(var j = 7; j >= 0; j--) |
| 340 | { |
| 341 | ifposDec(remainder, primePow[j]); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | var returnString = remainder.length + " AWAY TO CONVERT -> "; |
| 346 | for(var i = 0; i < remainder.length; i++) |
| 347 | { |
| 348 | returnString += remainder[i] + " "; |
| 349 | } |
| 350 | console.log(returnString); |
| 351 | |
| 352 | return convertNum(remainder); |
| 353 | } |
| 354 | |