⬆️ ⬇️

Integrating phpBB with a C # application

image



Good night, ladies and gentlemen!



I think everyone remembers how at one time the forums were popular and, of course, the forums on phpBB were popular. Today they, to my some regret, give way to social. networks, but not yet completely retreated into another world.

')

My post today will be about how I integrated phpBB with a C # application in terms of user authentication. I do not think that it will be interesting to many, but, as it seems to me, there will be people ...



First, open the includes \ functions.php file and find the functions phpbb_check_hash , _hash_crypt_private and _hash_encode64 . It is them that we have to port to C # and it is they who authenticate users in phpBB.



Let's connect the following libraries:



using System.Security.Cryptography; using System.Text; 




And we will begin to accurately translate functions into another language. I obviously did not change the names of functions and variables in accordance with the format adopted in C #, but left them the same as in phpBB.



  public bool phpbb_check_hash(string password, string hash) { var itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; if (hash.Length == 34) { return (_hash_crypt_private(password, hash, itoa64) == hash) ? true : false; } return (Md5(Encoding.UTF8.GetBytes(password)) == Encoding.UTF8.GetBytes(hash)) ? true : false; } 




  public string _hash_crypt_private(string password, string setting, string itoa64) { var output = "*"; if (setting.Substring(0, 3) != "$H$" && setting.Substring(0, 3) != "$P$") { return output; } var countLog2 = itoa64.IndexOf(setting[3]); if (countLog2 < 7 || countLog2 > 30) { return output; } var count = 1 << countLog2; var salt = setting.Substring(4, 8); if (salt.Length != 8) { return output; } var str = new byte[Encoding.UTF8.GetBytes(salt).Length + Encoding.UTF8.GetBytes(password).Length]; Array.Copy(Encoding.UTF8.GetBytes(salt), 0, str, 0, Encoding.UTF8.GetBytes(salt).Length); Array.Copy(Encoding.UTF8.GetBytes(password), 0, str, Encoding.UTF8.GetBytes(salt).Length, Encoding.UTF8.GetBytes(password).Length); var hash = Md5(str); do { str = new byte[hash.Length + Encoding.UTF8.GetBytes(password).Length]; Array.Copy(hash, 0, str, 0, hash.Length); Array.Copy(Encoding.UTF8.GetBytes(password), 0, str, hash.Length, Encoding.UTF8.GetBytes(password).Length); hash = Md5(str); } while (--count != 0); output = setting.Substring(0, 12); output += _hash_encode64(hash, 16, itoa64); return output; } 




  public string _hash_encode64(byte[] input, int count, string itoa64) { var output = ""; var i = 0; do { int value = input[i++]; output += itoa64[value & 0x3f]; if (i < count) { value |= input[i] << 8; } output += itoa64[(value >> 6) & 0x3f]; if (i++ >= count) { break; } if (i < count) { value |= input[i] << 16; } output += itoa64[(value >> 12) & 0x3f]; if (i++ >= count) { break; } output += itoa64[(value >> 18) & 0x3f]; } while (i < count); return output; } 




  public byte[] Md5(byte[] str) { var md5CryptoServiceProvider = new MD5CryptoServiceProvider(); return md5CryptoServiceProvider.ComputeHash(str); } 




I do not know if this code will be useful to someone, but at one time I rummaged through a lot of forums, but I didn’t find anything on this topic. That is why I decided to share the code with the public.



Thanks in advance for your comments!

Source: https://habr.com/ru/post/189240/



All Articles