You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
51 lines
1.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sog.Crypto
|
|
{
|
|
/// <summary>
|
|
/// tea密钥生成器
|
|
/// </summary>
|
|
public class TeaKeyGenerator : Singleton<TeaKeyGenerator>
|
|
{
|
|
Random m_random;
|
|
object m_locker = new object();
|
|
|
|
public TeaKeyGenerator()
|
|
{
|
|
m_random = new Random((int)DateTime.Now.Ticks);
|
|
}
|
|
|
|
//先简单点,随机
|
|
public byte[] GenerateNew()
|
|
{
|
|
//tea,xtea,xxtea的key是128bit,4个int,16byte
|
|
byte[] key = new byte[16];
|
|
|
|
//保证多线程安全
|
|
lock (m_locker)
|
|
{
|
|
m_random.NextBytes(key);
|
|
|
|
for (int i = 0; i < key.Length; i++)
|
|
{
|
|
while (key[i] == 0)
|
|
{
|
|
key[i] = (byte)m_random.Next(256);
|
|
}
|
|
}
|
|
}
|
|
|
|
return key;
|
|
}
|
|
|
|
public SogFastXTEAKey GenerateFastXTEAKey()
|
|
{
|
|
byte[] keyBytes = GenerateNew();
|
|
|
|
return new SogFastXTEAKey(keyBytes);
|
|
}
|
|
}
|
|
}
|
|
|