/// <summary> /// Initializes a new instance of the <see cref="SessionCrypter" /> class. /// </summary> /// <param name="keyDecrypter">The key decrypter.</param> /// <param name="sessionMaterial">The session material.</param> /// <param name="verifier">The verifier, optionally used to certify sender. (Equivialent to SignedSessionDecrypter)</param> /// <param name="keyPacker">The key packer.</param> public SessionCrypter(Crypter keyDecrypter, WebBase64 sessionMaterial, AttachedVerifier verifier = null, ISessionKeyPacker keyPacker = null) { Workings initLazy() { var workings = new Workings(); if (verifier != null) { keyPacker = keyPacker ?? new NonceSignedSessionPacker(); } keyPacker = keyPacker ?? new SimpleAesHmacSha1KeyPacker(); var sessionMaterialBytes = sessionMaterial.ToBytes(); var sessionPacker = keyPacker as IInteroperableSessionMaterialPacker; workings._verifier = verifier; if (sessionPacker == null && workings._verifier != null) { sessionMaterialBytes = workings._verifier.VerifiedMessage(sessionMaterialBytes); } var packedBytes = keyDecrypter.Decrypt(sessionMaterialBytes); Key key; if (sessionPacker == null) { key = keyPacker.Unpack(packedBytes, Config); } else { var nonceSession = sessionPacker.UnpackMaterial(packedBytes, Config); key = nonceSession.Key; workings._nonce = nonceSession.Nonce.ToBytes(); } workings._keyset = new ImportedKeySet(key, KeyPurpose.DecryptAndEncrypt); workings._crypter = new Crypter(workings._keyset); workings._sessionMaterial = sessionMaterial; return(workings); } _working = new Lazy <Workings>(initLazy); }
/// <summary> /// Initializes a new instance of the <see cref="SessionCrypter" /> class. /// </summary> /// <param name="keyDecrypter">The key decrypter.</param> /// <param name="sessionMaterial">The session material.</param> /// <param name="verifier">The verifier, optionally used to certify sender. (Equivialent to SignedSessionDecrypter)</param> /// <param name="keyPacker">The key packer.</param> public SessionCrypter(Crypter keyDecrypter, WebBase64 sessionMaterial, AttachedVerifier verifier = null, ISessionKeyPacker keyPacker = null) { if (verifier != null) { keyPacker = keyPacker ?? new NonceSignedSessionPacker(); } keyPacker = keyPacker ?? new SimpleAesHmacSha1KeyPacker(); var sessionMaterialBytes = sessionMaterial.ToBytes(); var sessionPacker = keyPacker as IInteroperableSessionMaterialPacker; _verifier = verifier; if (sessionPacker == null && _verifier != null) { sessionMaterialBytes = _verifier.VerifiedMessage(sessionMaterialBytes); } var packedBytes = keyDecrypter.Decrypt(sessionMaterialBytes); Key key; if (sessionPacker == null) { key = keyPacker.Unpack(packedBytes); } else { var nonceSession = sessionPacker.UnpackMaterial(packedBytes); key = nonceSession.Key; _nonce = nonceSession.Nonce.ToBytes(); } _keyset = new ImportedKeySet(key, KeyPurpose.DecryptAndEncrypt); _crypter = new Crypter(_keyset); _sessionMaterial = sessionMaterial; }