using System.Security.Cryptography;
using System;

namespace Com.Bocom.OpenApi
{
    namespace Utils
    {
        public class KeyGeneratorUtils
        {
            /// <summary>
            /// 随机生成秘钥（对称算法）
            /// </summary>
            /// <param name="key">秘钥(base64格式)</param>
            /// <param name="iv">iv向量(base64格式)</param>
            /// <param name="keySize">要生成的KeySize，每8个byte是一个字节，注意每种算法支持的KeySize均有差异，实际可通过输出LegalKeySizes来得到支持的值</param>
            public static void CreateSymmetricAlgorithmKey<T>(out string key, out string iv, int keySize)
                where T : SymmetricAlgorithm, new()
            {
                using (T t = new T())
                {
                    t.KeySize = keySize;
                    t.GenerateIV();
                    t.GenerateKey();
                    iv = Convert.ToBase64String(t.IV);
                    key = Convert.ToBase64String(t.Key);
                }
            }
        }
    }
}