using System; using System.IO; using System.Text; using System.Collections.Generic; namespace UIDGEN { class Program { /// /// AES128-CBCモードで使用する鍵、初期ベクタをここで指定します。 /// 以下に KEY=16byte / IV=16byte のテスト用データを設定しています。 /// internal static byte[] key = { 0x51,0xC6,0x6D,0xA2,0x6F,0x27,0x80,0xE3,0xDA,0xF2,0x72,0xE1,0x34,0xAC,0xCC,0x1B }; internal static byte[] iv = { 0x58,0xA3,0x2F, 0xAA,0xC7,0xA6, 0x56,0x1A, 0xE1, 0x5,0xAF,0x47,0xF1,0xD3,0x79,0xB1 }; /// /// こちらでメーカーコード、生成数などを設定します。 /// internal static byte MAKER_CODE = 0x05; // メーカーコード internal static Int64 INITIAL_ID = 0x3F00001234; // 40bit の シリアル番号(0x3F + 0x********) internal static int LIST_ITEMS = 30; // 生成するUID数(=30個) /// /// メインプログラム /// /// /// static int Main(string[] args) { // 出力先ファイルを開きます System.IO.FileStream fw = new System.IO.FileStream("UIDLIST.txt", FileMode.Create); System.IO.TextWriter tw = new System.IO.StreamWriter(fw); // LIST_ITEMS個数分のデータを生成します。 // INITIAL_ID ~ INITIAL_ID+LIST_ITEMS-1 の数量分を生成します for (int i = 0; i < LIST_ITEMS; i++) { byte[] result = UIDGEN.GeterateUID(CreateTemporaryId(MAKER_CODE, INITIAL_ID + i)); // コンソールに出力します Console.WriteLine(BitConverter.ToString(result)); // ファイルに出力します tw.WriteLine(BitConverter.ToString(result)); } tw.Flush(); tw.Dispose(); fw.Close(); fw.Dispose(); return 0; } // 乱数発生器 static System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); /// /// 8バイトの中間Idを生成します。乱数2バイトは自動生成されます。 /// /// メーカーコード /// シーケンシャルId /// static private byte[] CreateTemporaryId(byte makerCode, Int64 sequentialId) { byte[] Id = new byte[8]; rng.GetBytes(Id); Id[0] = makerCode; Id[1] = (byte)((sequentialId >> 32) & 0xFF); Id[2] = (byte)((sequentialId >> 24) & 0xFF); Id[3] = (byte)((sequentialId >> 16) & 0xFF); Id[4] = (byte)((sequentialId >> 8) & 0xFF); Id[5] = (byte)((sequentialId >> 0) & 0xFF); return Id; } } }