fifa code

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
					
public class Program
{
	public static void Main()
	{
		var fifacode = Scan("http://fifapaninicode7.appspot.com/");
		Console.WriteLine(fifacode);
		
		var code = BaseConvert(fifacode.Code.Replace("-", string.Empty), 34, 10);
		Console.WriteLine("Code: {0}", code);
		
		var prefix = BaseConvert(fifacode.Prefix.Replace("-", string.Empty), 34, 10);
		Console.WriteLine("Prefix: {0}", prefix);
		
		var suffix = BaseConvert(fifacode.Suffix, 34, 10);
		Console.WriteLine("Suffix: {0}", suffix);

		var newCode = BaseConvert(prefix.ToString(), 10, 34);
		Console.WriteLine(newCode);
		
		var test = GetMod10Digit(code);
		Console.WriteLine(test);
		
		string theString = "asdf";

		string hash;
		using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) {
		  hash = BitConverter.ToString(
			md5.ComputeHash(Encoding.UTF8.GetBytes(theString))
		  ).Replace("-", String.Empty);
		}
		
		Console.WriteLine(hash);
	}
	
	public static string BaseConvert(string number, int fromBase, int toBase)
	{
	    var digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	    var length = number.Length;
	    var result = string.Empty;
	
	    var nibbles = number.Select(c => digits.IndexOf(c)).ToList();
	    int newlen;
	    do {
	        var value = 0;
	        newlen = 0;
	
	        for (var i = 0; i < length; ++i) {
	            value = value * fromBase + nibbles[i];
	            if (value >= toBase) {
	                if (newlen == nibbles.Count) {
	                    nibbles.Add(0);
	                }
	                nibbles[newlen++] = value / toBase;
	                value %= toBase;
	            }
	            else if (newlen > 0) {
	                if (newlen == nibbles.Count) {
	                    nibbles.Add(0);
	                }
	                nibbles[newlen++] = 0;
	            }
	        }
	        length = newlen;
	        result = digits[value] + result; //
	    }
	    while (newlen != 0);
	
	    return result;
	}
	
	public static FifaCode Scan(string url)
	{
		var objRequest = (HttpWebRequest)HttpWebRequest.Create(url);
		objRequest.Method = WebRequestMethods.Http.Get;
		var objResponse = (HttpWebResponse)objRequest.GetResponse();
		var reader = new StreamReader(objResponse.GetResponseStream());
		var tmp = reader.ReadToEnd();
		objResponse.Close();
		
		var m = Regex.Match(tmp, "(?<code>(?<prefix>[A-Z0-9]{4}-[A-Z0-9]{4}?)-(?<suffix>[A-Z0-9]{4}?)?)");
		
		return new FifaCode(m.Groups["code"].Value, m.Groups["prefix"].Value, m.Groups["suffix"].Value);
	}
	
	public static int GetMod10Digit(string data)
	{
		int sum = 0;
		bool odd = true;
		for (int i = data.Length - 1; i >= 0; i--)
		{
			if (odd == true)
			{
				int tSum = Convert.ToInt32(data[i].ToString()) * 2;
				if (tSum >= 10)
				{
					string tData = tSum.ToString();
					tSum = Convert.ToInt32(tData[0].ToString()) + Convert.ToInt32(tData[1].ToString());
				}
				sum += tSum;
			}
			else
				sum += Convert.ToInt32(data[i].ToString());
			odd = !odd;
		}
		
		int result = (((sum / 10) + 1) * 10) - sum;
		return result % 10;
	}
	
	public class FifaCode {
		public string Code { get; set; }
		public string Prefix { get; set; }
		public string Suffix { get; set; }
		
		public FifaCode(string code, string prefix, string suffix)
		{
			this.Code = code;
			this.Prefix = prefix;
			this.Suffix = suffix;
		}
		
		public override string ToString()
		{
			return string.Format("Code: {0}, Prefix: {1}, Suffix: {2}", this.Code, this.Prefix, this.Suffix);	
		}
	}
}
;