/* Copyright (C) Itseez3D, Inc. - All Rights Reserved
* You may not use this file except in compliance with an authorized license
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* UNLESS REQUIRED BY APPLICABLE LAW OR AGREED BY ITSEEZ3D, INC. IN WRITING, SOFTWARE DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED
* See the License for the specific language governing permissions and limitations under the License.
* Written by Itseez3D, Inc. <support@avatarsdk.com>, August 2026
*/

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

namespace AvatarSDKMove
{
	public static class EncryptionUtils
	{
		public static string Encrypt(string s, string key)
		{
			if (key.Length != 32)
				throw new Exception("32 character string is required as a key");

			byte[] bytes = Encoding.UTF8.GetBytes(s);
			using (var algo = new RijndaelManaged())
			{
				algo.Key = Encoding.UTF8.GetBytes(key);
				algo.Mode = CipherMode.ECB;
				algo.Padding = PaddingMode.PKCS7;
				byte[] result = algo.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
				return Convert.ToBase64String(result, 0, result.Length);
			}
		}

		public static string Decrypt(string encrypted, string key)
		{
			if (key.Length != 32)
				throw new Exception("32 character string is required as a key");

			byte[] bytes = Convert.FromBase64String(encrypted);
			using (var algo = new RijndaelManaged())
			{
				algo.Key = Encoding.UTF8.GetBytes(key);
				algo.Mode = CipherMode.ECB;
				algo.Padding = PaddingMode.PKCS7;
				byte[] result = algo.CreateDecryptor().TransformFinalBlock(bytes, 0, bytes.Length);
				return Encoding.UTF8.GetString(result);
			}
		}

		public static string GetMd5Hash(string input)
		{
			using (MD5 md5 = MD5.Create())
			{
				byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(input));

				var sb = new StringBuilder();
				for (int i = 0; i < hashBytes.Length; i++)
					sb.Append(hashBytes[i].ToString("x2"));
				return sb.ToString();
			}
		}
	}
}
