Exemplo n.º 1
0
        internal X509Certificate2 CreateCertificate(
            X509Certificate2 issuer,
            X500DistinguishedName subject,
            IEnumerable <X509Extension> extensions,
            DateTimeOffset notBefore,
            DateTimeOffset notAfter, out RSA key)
        {
            key = CreateKeyMaterial(RSAMinimumKeySizeInBits);

            var request = new CertificateRequest(subject, key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

            foreach (var extension in extensions)
            {
                request.CertificateExtensions.Add(extension);
            }

            var serialNumber = new byte[20];
            var random       = new RandomBigInteger();

            random.NextBytes(serialNumber);
            return(request.Create(issuer, notBefore, notAfter, serialNumber));

            RSA CreateKeyMaterial(int minimumKeySize)
            {
                var rsa = RSA.Create(minimumKeySize);

                if (rsa.KeySize < minimumKeySize)
                {
                    throw new InvalidOperationException($"Failed to create a key with a size of {minimumKeySize} bits");
                }

                return(rsa);
            }
        }
Exemplo n.º 2
0
        private static BigInteger createModel(byte[] owner, string properties, string hash)
        {
            StorageContext ctx = Storage.CurrentContext;

            Model model = new Model();

            model.owner      = owner;
            model.properties = properties;
            model.hash       = hash;

            int seed             = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            RandomBigInteger rnd = new RandomBigInteger(seed);
            BigInteger       id  = rnd.NextBigInteger(63);

            while (Storage.Get(ctx, concatKey("models/", id)).Length != 0)
            {
                id = rnd.NextBigInteger(63);
            }
            model.id = id;
            byte[] key = concatKey("models/", id);

            byte[] obj = Model.ToByteArray(model);

            Storage.Put(ctx, key, obj);
            return(id);
        }
Exemplo n.º 3
0
        public static void KeyGen(int key_size, out BigInteger oe, out BigInteger od, out BigInteger on)
        {
            RandomBigInteger rand = new RandomBigInteger();
            BigInteger       p    = rand.NextBigInteger(key_size / 2);

            p = p.GetNextPrime();

            int        qBitLen = key_size - p.BitLength();
            BigInteger q, n;

            do
            {
                q = rand.NextBigInteger(qBitLen);
                q = q.GetNextPrime();
                n = p * q;
                qBitLen++;
            } while (n.BitLength() < key_size + 1);

            BigInteger phi = (p - 1) * (q - 1);
            BigInteger e   = 3;

            while (BigInteger.GreatestCommonDivisor(e, phi) != 1)
            {
                e += 2;
            }

            BigInteger d = (e.ModInverse(phi));

            oe = e;
            od = d;
            on = n;
        }
Exemplo n.º 4
0
        BigInteger RandomWithGCD1(BigInteger min, BigInteger max)
        {
            RandomBigInteger rbi   = new RandomBigInteger();
            BigInteger       value = 0;

            do
            {
                value = rbi.NextBigInteger(min, max);
                BigInteger a = BigInteger.GreatestCommonDivisor(value, max);
            } while (BigInteger.GreatestCommonDivisor(value, max) != 1);
            return(value);
        }
Exemplo n.º 5
0
 protected Task <BigInteger> RandomIntegerBelowAsync(BigInteger max)
 {
     return(RandomBigInteger.GenerateAsync(max, this.RandomProvider));
 }