/// <summary> /// Generates the key. /// </summary> /// <param name="size">The size.</param> protected override void GenerateKey(int size, KeyczarConfig config) { AesKeyBytes = new byte[size / 8]; Secure.Random.NextBytes(AesKeyBytes); HmacKey = (HmacSha2Key)Generate(UnofficialKeyType.HmacSha2, size); HmacKey.HashLength = size / 8; //Truncate hash length to match JWE //https://tools.ietf.org/html/rfc7518#page-23 }
/// <summary> /// Generates the key. /// </summary> /// <param name="size">The size.</param> protected override void GenerateKey(int size, KeyczarConfig config) { HmacKeyBytes = new byte[size / 8]; Digest = DigestForSize(size); HashLength = HashLengthForDigest(Digest); Secure.Random.NextBytes(HmacKeyBytes); }
/// <summary> /// Generates the key. /// </summary> /// <param name="size">The size.</param> protected override void GenerateKey(int size, KeyczarConfig config) { var rsaparam = new RsaKeyPairGenerator(); rsaparam.Init(new KeyGenerationParameters(Secure.Random, size)); var pair = rsaparam.GenerateKeyPair(); var priv = (RsaPrivateCrtKeyParameters)pair.Private; PrivateExponent = priv.Exponent.ToSystemBigInteger(); PrimeP = priv.P.ToSystemBigInteger(); PrimeQ = priv.Q.ToSystemBigInteger(); PrimeExponentP = priv.DP.ToSystemBigInteger(); PrimeExponentQ = priv.DQ.ToSystemBigInteger(); CrtCoefficient = priv.QInv.ToSystemBigInteger(); var pub = (RsaKeyParameters)pair.Public; PublicKey = GeneratePubKey(size, pub.Exponent.ToSystemBigInteger(), pub.Modulus.ToSystemBigInteger()); }
/// <summary> /// Generates the key. /// </summary> /// <param name="size">The size.</param> protected override void GenerateKey(int size, KeyczarConfig config) { var paramgen = new DsaParametersGenerator(); paramgen.Init(size, 100, Secure.Random); var keygen = new DsaKeyPairGenerator(); keygen.Init(new DsaKeyGenerationParameters(Secure.Random, paramgen.GenerateParameters())); var pair = keygen.GenerateKeyPair(); var priv = (DsaPrivateKeyParameters)pair.Private; X = priv.X.ToSystemBigInteger(); Size = size; PublicKey = new DsaPublicKey(); var pub = (DsaPublicKeyParameters)pair.Public; PublicKey.Y = pub.Y.ToSystemBigInteger(); PublicKey.G = pub.Parameters.G.ToSystemBigInteger(); PublicKey.P = pub.Parameters.P.ToSystemBigInteger(); PublicKey.Q = pub.Parameters.Q.ToSystemBigInteger(); PublicKey.Size = size; }
/// <summary> /// Unpacks the specified bytes into a key. /// </summary> /// <param name="data">The bytes.</param> /// <returns></returns> public Key Unpack(byte[] data, KeyczarConfig config) { using (var input = new MemoryStream(data)) using (var reader = new BsonDataReader(input)) { var val = JToken.ReadFrom(reader); var keyType = (KeyType)(string)val["type"]; var keyString = val["key"].ToString(); using (var stringReader = new StringReader(keyString)) { var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver () }); return ((Key) jsonSerializer.Deserialize(new WebSafeBase64ByteConverter.RawJsonReader(stringReader), keyType.RepresentedType)); } } }
protected override void GenerateKey(int size, KeyczarConfig config) { throw new NotImplementedException(); }
/// <summary> /// Generates the key. /// </summary> /// <param name="size">The size.</param> protected override void GenerateKey(int size, KeyczarConfig config) { AesKeyBytes = new byte[size / 8]; Secure.Random.NextBytes(AesKeyBytes); HmacKey = (HmacSha1Key)Generate(KeyType.HmacSha1, 0 /*uses default size*/); }
/// <summary> /// Packs the specified key into bytes /// </summary> /// <param name="key">The key.</param> /// <returns></returns> public byte[] Pack(Key key, KeyczarConfig config) => Utility.ToBson(PackIt(key));
/// <summary> /// Generates the key. /// </summary> /// <param name="size">The size.</param> /// <exception cref="System.NotSupportedException"></exception> protected override void GenerateKey(int size, KeyczarConfig config) => throw new NotSupportedException();
/// <summary> /// Generates the key. /// </summary> /// <param name="size">The size.</param> protected override void GenerateKey(int size, KeyczarConfig config) { AesKeyBytes = new byte[size / 8]; Secure.Random.NextBytes(AesKeyBytes); }