Exemplo n.º 1
0
        static async Task Main()
        {
            var primeCore = new PrimeCoreKey
            {
                //overrides default values
                CoreKeyConfig = new CoreKeyConfig
                {
                    Length  = 50,
                    UseSalt = false,
                    CoreKeyFormatterConfig = new CoreKeyFormatterConfig
                    {
                        Format           = PrimeFormatter.Ignore,
                        IsStrictedLength = false
                    }
                }
            };

            var primeKey = primeCore.CreateKey();


            Console.WriteLine(primeKey.Result);
            Console.WriteLine(primeCore.IsSalted(primeKey));

            var re = primeCore.ReKey(primeKey);

            Console.WriteLine(re);

            primeCore.AddToIgnore(new CoreKey[1] {
                primeKey
            });

            await foreach (var item in primeCore.ReadIgnoredKeysAsync())
            {
                Console.WriteLine(item.Result);
            }

            var keyDescriptor = new KeyDescriptor
            {
                SecretKey = new SecretKey
                {
                    Secret = "superSecretKey"
                },
                KeySignature = new KeySignature
                {
                    Signature = "mycoolapp"
                }
            };

            var ke  = new PrimeIdentityKey(keyDescriptor);
            var key = ke.CreateKey("test");

            Console.WriteLine(key.Result);
            var result  = ke.WritePayload();
            var unknown = ke.WritePayload(keyDescriptor, key.Result);

            Console.WriteLine(result);
            Console.WriteLine(unknown);
        }
Exemplo n.º 2
0
        internal static CoreKey NewKey(this PrimeCoreKey prime, CoreKeyConfig config)
        {
            var maxLength  = config.Length;
            var keyBuilder = new StringBuilder();
            var format     = config.CoreKeyFormatterConfig;

            for (int i = 0; i <= maxLength; i++)
            {
                GeneratedKeys(out string firstSmallChar, out string secondSmallChar, out int num, out string bigChar);

                var combined = $"{firstSmallChar}{secondSmallChar}{num}{bigChar}";
                if (config.UseSalt)
                {
                    keyBuilder.Append(KeySalt.RNGCrypto(combined));
                }
                keyBuilder.Append(combined);

                if (keyBuilder.Length > config.Length)
                {
                    keyBuilder.Remove(maxLength, keyBuilder.Length - maxLength);
                    break;
                }
            }
            if (PrimeStore.IgnoredKeys.Any(x => x.Result == keyBuilder.ToString()))
            {
                NewKey(prime, config);
            }

            if (format.Format == PrimeFormatter.Ignore || format.Format == null)
            {
                return new CoreKey {
                           Result = keyBuilder.ToString()
                }
            }
            ;

            if (format.IsStrictedLength)
            {
                return new CoreKey {
                           Result = KeyFormatter.UseUnderscore(keyBuilder.ToString(), config)
                }
            }
            ;

            return(new CoreKey {
                Result = KeyFormatter.UseUnderscore(keyBuilder.ToString())
            });
        }
Exemplo n.º 3
0
        internal static string Refactor(this PrimeCoreKey prime, CoreKeyConfig primeCofing, CoreKey key)
        {
            var _      = key.Result;
            var __     = _.ToCharArray();
            var config = primeCofing;
            var format = primeCofing.CoreKeyFormatterConfig;

            var keyBuilder = new StringBuilder();

            for (int i = 0; i < __.Length; i++)
            {
                var charRandom      = new Random().Next(0, 26);
                var numRandom       = new Random().Next(0, 10);
                var firstSmallChar  = PrimeStore.chars[charRandom];
                var secondSmallChar = PrimeStore.chars[charRandom.ReInvoke()];
                var num             = PrimeStore.nums[numRandom] * 2;
                var bigChar         = PrimeStore.chars[charRandom.ReInvoke()].ToUpper();
                if (_.Length - i != 4)
                {
                    continue;
                }
                else
                {
                    var combine = new char[] { __[i], __[i + 1], __[i + 2], __[i + 3] };
                    if (config.UseSalt)
                    {
                        keyBuilder.Append(new string(combine));
                    }

                    __[i]     = char.Parse(firstSmallChar);
                    __[i + 1] = char.Parse(secondSmallChar);
                    __[i + 2] = (char)num;
                    __[i + 3] = char.Parse(bigChar);
                    keyBuilder.Append(combine);
                }
                if (keyBuilder.Length >= config.Length)
                {
                    keyBuilder.Remove(config.Length, keyBuilder.Length - config.Length);
                    break;
                }
            }
            if (PrimeStore.IgnoredKeys.Any(x => x.Result == keyBuilder.ToString()))
            {
                Refactor(prime, config, key);
            }

            if (keyBuilder.Length < config.Length) //fail
            {
                return(NewKey(prime, config).Result);
            }

            if (format.Format == PrimeFormatter.Ignore || format.Format == null)
            {
                return(keyBuilder.ToString());
            }


            if (format.IsStrictedLength)
            {
                return(KeyFormatter.UseUnderscore(keyBuilder.ToString(), config));
            }

            return(KeyFormatter.UseUnderscore(keyBuilder.ToString()));
        }