static bool verify(Key key, Signature signature, byte[] data) { using (var dsa = useSigningService(key)) { return dsa.verify(data, signature); } }
static Key removePrivateKey(Key key) { using (var dsa = useSigningService(key)) { return dsa.exportKey(false); } }
static Signature sign(Key key, byte[] data) { using (var dsa = useSigningService(key)) { var signed = dsa.sign(data); return new Signature(new SignatureFormat(key.Format, DefaultHashAlgorithm), signed); } }
public EncryptionService(Key key) { _provider = new AesCryptoServiceProvider { KeySize = key.Format.BitSize.signed(), Key = key.Data }; _outputStream = new ReusableMemoryStream(); }
// Create an object of type IV from a given object of type Key. // Usage of AesCryptoServiceProvider: // - create it // - set its key size according the the size of the input Key // - use GenerateIV() public static IV createRandomIV(Key key) { using (var aes = new AesCryptoServiceProvider()) { aes.KeySize = key.Format.BitSize.signed(); // this should not be required? // aes.Key = key.Data; aes.GenerateIV(); return new IV(aes.IV); } }
public bool isSame(Key other) { return Format == other.Format && isSame(Data, other.Data); }
static ISigningService useSigningService(Key key) { switch (key.Format.Algorithm) { case DSASigningService.AlgorithmName: return new DSASigningService(key); } throw new InternalError("Unsupported key algorithm {0}".format(key.Format.Algorithm)); }
static bool hasPrivateKey(Key key) { var without = removePrivateKey(key); return !without.isSame(key); }
// Return decrypted data by using DecryptionService and the given Key public static BufferReference decrypt(Key key, BufferReference content) { using (var service = new DecryptionService(key)) { return service.decrypt(content); } }
// Return decrypted data by using DecryptionService and the given Key public static BufferReference decrypt(Key key, byte[] content) { return decrypt(key, content.asBufferReference()); }
public static BufferReference encrypt(Key key, IV? iv_, BufferReference content) { using (var service = new EncryptionService(key)) { var iv = iv_ ?? service.createRandomIV(); return service.encrypt(iv, content); } }
public static BufferReference encrypt(Key key, BufferReference content) { return encrypt(key, null, content); }
public DSASigningService(Key key) { Debug.Assert(key.Format.Algorithm == AlgorithmName); _provider = new DSACryptoServiceProvider(key.Format.BitSize.signed()); _provider.FromXmlString(System.Text.Encoding.UTF8.GetString(key.Data)); }