public string Generate() { byte[] bytes = new byte[20]; RandomNumberGenerator.Fill(bytes); return(Base32Converter.ToBase32(bytes)); }
static void Main(string[] args) { //var v = " 12.31%".ExtractDecimal(); //var v = DateTimeHelper.GetMonthStartDatesInRange("2020.01.01".ToDateTime(), "2020.03.30".ToDateTime()); //string v = (12.31).ConvertToSqlParameter(true); string n1 = Base32Converter.Encode(532); long n2 = Base32Converter.Decode("777"); }
public bool TryDecode(string code, out ProductLicense license) { // License format: // No. Bytes Field Byte index // 1 X 0 // 1 License flags 1 // 1 VersionNoApplicable 2 // 2 ExpirationDate 3 // 1 Y 5 // 1 ExpirationDays 6 // 1 ExpirationLoads 7 // 1 MaxSimultaneousUsers 8 // 1 Z 9 // X = random(0,255) // Y = (byte[1] + byte[2] + byte[3] + byte[6] + byte[7] + byte[8]) % 255 // Z = (X^2 + Y^2) % 255 license = new ProductLicense(); if (string.IsNullOrEmpty(code)) { return(false); } byte[] bytes = Base32Converter.FromBase32String(code.Replace("-", string.Empty)); if (bytes.Length != 10) { return(false); } bytes = bytes.Xor(ProductInformationServices.ProductInformation.ProductCode.ToByteArray()); // apply internal mask StandardLicenseCodecTools.ApplyInternalMask(bytes); int X = (int)bytes[0]; int Y = (int)bytes[5]; int Z = (int)bytes[9]; if (Y != (((int)bytes[1]) + ((int)bytes[2]) + ((int)bytes[3]) + ((int)bytes[6]) + ((int)bytes[7]) + ((int)bytes[8])) % 255) { return(false); } if ((X * X + Y * Y) % 255 != Z) { return(false); } license.Flags = (ProductLicenseFlags)bytes[1]; license.MajorVersionApplicable = bytes[2]; license.ExpirationDate = StandardLicenseCodecTools.FromDaysSince2008Jan1(BitConverter.ToUInt16(bytes, 3)); license.ExpirationDays = bytes[6]; license.ExpirationLoads = bytes[7]; license.MaxSimultaneousUsers = bytes[8]; return(true); }
public string Generate() { var bytes = new byte[32]; RandomNumberGenerator.Fill(bytes); var token = Base32Converter .ToBase32(bytes) .TrimEnd('=') .ToLowerInvariant(); return(token); }
public string GenerateKey(ProductLicense license) { // License format: // No. Bytes Field Byte index // 1 X 0 // 1 License flags 1 // 1 VersionNoApplicable 2 // 2 ExpirationDate 3 // 1 Y 5 // 1 ExpirationDays 6 // 1 ExpirationLoads 7 // 1 MaxSimultaneousUsers 8 // 1 Z 9 // X = random(0,255) // Y = (byte[1] + byte[2] + byte[3] + byte[6] + byte[7] + byte[8]) % 255 // Z = (X^2 + Y^2) % 255 byte[] byteArray = new byte[10]; Tools.Maths.RandomNumberGenerator.NextBytes(byteArray); byteArray[1] = (byte)license.Flags; if (license.VersionAware) { byteArray[2] = license.MajorVersionApplicable; } if (license.ExpiresAfterDate) { Array.Copy( BitConverter.GetBytes(StandardLicenseCodecTools.ToDaysSince2008Jan1(license.ExpirationDate)), 0, byteArray, 3, 2 ); } if (license.ExpiresAfterDays) { byteArray[6] = license.ExpirationDays; } if (license.ExpiresAfterLoads) { byteArray[7] = license.ExpirationLoads; } if (license.LimitSimultaneousUsers) { byteArray[8] = license.MaxSimultaneousUsers; } // add in license validity bytes int X = Tools.Maths.RandomNumberGenerator.Next(0, 255); int Y = (((int)byteArray[1]) + ((int)byteArray[2]) + ((int)byteArray[3]) + ((int)byteArray[6]) + ((int)byteArray[7]) + ((int)byteArray[8])) % 255; int Z = (X * X + Y * Y) % 255; byteArray[0] = (byte)X; byteArray[5] = (byte)Y; byteArray[9] = (byte)Z; // apply internal mask StandardLicenseCodecTools.ApplyInternalMask(byteArray); // apply product code mask byteArray = byteArray.Xor(ProductInformationServices.ProductInformation.ProductCode.ToByteArray()); return(Beutify(Base32Converter.ToBase32String(byteArray))); }
public static byte[] FromBase32(string text) { return(Base32Converter.FromBase32String(text)); }
public static string ToBase32(string text) { return(Base32Converter.ToBase32String(text)); }