/// <summary> /// Checks if the file signature is valid. /// </summary> /// <param name="data">Original, decrypted file in raw format.</param> /// <param name="userId">Id of the user decrypting the file.</param> /// <param name="ownerPublicKey">Public RSA key of the file owner used to verify file signature.</param> /// <returns>true if the signature is valid, otherwise false.</returns> private bool CheckFileSignature(byte[] data, int userId, RSAParameters ownerPublicKey) { try { var hashAlgo = AlgorithmUtility.GetHashAlgoFromNameSignature(((SecurityDescriptor)Headers[1]).HashAlgorithmName); return(CheckFileSignatureHelper(data, userId, ownerPublicKey, hashAlgo)); } catch (CryptographicException) { var hashAlgo = AlgorithmUtility.GetHashSignerFromNameSignature(((SecurityDescriptor)Headers[1]).HashAlgorithmName); return(CheckFileSignatureHelper(data, userId, ownerPublicKey, hashAlgo)); } }
/// <summary> /// Creates a file signature using the unencripted data and user private RSA key. /// </summary> /// <param name="data">Original, unencrypted file in raw format.</param> /// <param name="userPrivateKey">Private RSA key of the user encrypting the file.</param> private void SignFile(byte[] data, ref RSAParameters userPrivateKey) { try { // Exception will be thrown if the hashing algoritm is MD2, MD4, RIPEMD or SHA224. var hashAlgo = AlgorithmUtility.GetHashAlgoFromNameSignature(((SecurityDescriptor)Headers[1]).HashAlgorithmName); ((SecurityDescriptor)Headers[1]).Signature = new RsaAlgorithm(userPrivateKey).CreateSignature(data, hashAlgo); } catch (CryptographicException) { var hashAlgo = AlgorithmUtility.GetHashSignerFromNameSignature(((SecurityDescriptor)Headers[1]).HashAlgorithmName); ((SecurityDescriptor)Headers[1]).Signature = new RsaAlgorithm(userPrivateKey).CreateSignature(data, hashAlgo); } }