コード例 #1
0
ファイル: GpgContext.cs プロジェクト: joanbarros/Gpg.NET
        /// <summary>
        /// Gets the GPG key associated with the given key ID.
        /// This method will throw an exception if the given key ID matches zero or multiple keys.
        /// </summary>
        /// <param name="keyId">The key ID of the requested GPG key.</param>
        /// <exception cref="GpgKeyNotFoundException">
        /// Thrown when the given key ID matches zero or multiple keys.
        /// </exception>
        public GpgKey GetKey(string keyId)
        {
            var matches = GpgMeHelper.FindKeys(this, keyId, false).ToArray();

            if (matches.Length == 0)
            {
                throw new GpgKeyNotFoundException("No matches were returned for the given key ID");
            }
            if (matches.Length > 1)
            {
                throw new GpgKeyNotFoundException("Multiple matches were found for the given key ID");
            }
            return(matches[0]);
        }
コード例 #2
0
ファイル: GpgNet.cs プロジェクト: joanbarros/Gpg.NET
        /// <summary>
        /// Initialises the underlying GpgME library.
        /// </summary>
        /// <param name="dllPath">When set, overrides the default GpgME DLL location.</param>
        /// <param name="installDir">When set, overrides the default GpgME installation directory.
        /// This should point to the bin directory of the GPG installation directory.</param>
        /// <param name="minLibraryVersion">
        /// The minimum required version of GpgME. Set to null to disable this version check.
        /// </param>
        /// <param name="minGpgVersion">
        /// The minimum required version of Gpg. Set to null to disable this version check.
        /// </param>
        public static void Initialise(string dllPath = null, string installDir = null, string minLibraryVersion = "1.8.0", string minGpgVersion = "2.0.0")
        {
            if (Initialised)
            {
                throw new InvalidOperationException("GpgME has already been initialised.");
            }
            if (dllPath != null)
            {
                // Manually load the GpgME DL from a custom path
                // instead of letting Windows look for it.
                Kernel32.Load(dllPath);
            }

            // Global flags should be set before initialisation
            if (minGpgVersion != null)
            {
                if (GpgMeWrapper.gpgme_set_global_flag("require-gnupg", minGpgVersion) != 0)
                {
                    throw new GpgNetException("Failed to set minimum required GnuPG version.");
                }
            }
            if (installDir != null)
            {
                var result = GpgMeWrapper.gpgme_set_global_flag("w32-inst-dir", installDir);
                if (result != 0)
                {
                    throw new GpgNetException("Failed to set Win32 install dir.");
                }
            }
            // The version check is required as it initialises GpgME
            Version = GpgMeWrapper.gpgme_check_version(minLibraryVersion);
            if (Version == null)
            {
                throw new GpgNetException("Minimum required GpgME version is not met.");
            }

            // Get information about the GPG engines available
            AvailableEngines = GpgMeHelper.GetEngines();
            // Get information about the current GPG configuration
            Homedir     = GpgMeWrapper.gpgme_get_dirinfo("homedir");
            Sysconfdir  = GpgMeWrapper.gpgme_get_dirinfo("sysconfdir");
            Bindir      = GpgMeWrapper.gpgme_get_dirinfo("bindir");
            Libdir      = GpgMeWrapper.gpgme_get_dirinfo("libdir");
            Libexecdir  = GpgMeWrapper.gpgme_get_dirinfo("libexecdir");
            Datadir     = GpgMeWrapper.gpgme_get_dirinfo("datadir");
            Localedir   = GpgMeWrapper.gpgme_get_dirinfo("localedir");
            AgentSocket = GpgMeWrapper.gpgme_get_dirinfo("agent-socket");
            Initialised = true;
        }
コード例 #3
0
ファイル: GpgBuffer.cs プロジェクト: joanbarros/Gpg.NET
 /// <summary>
 /// Writes a sequence of bytes from the current stream and advances the position
 /// within the stream by the number of bytes written.
 /// </summary>
 public override void Write(byte[] buffer, int offset, int count)
 {
     GpgMeHelper.Write(Handle, buffer, offset, count);
 }
コード例 #4
0
ファイル: GpgBuffer.cs プロジェクト: joanbarros/Gpg.NET
 /// <summary>
 /// Reads a sequence of bytes from the current stream and advances the position
 /// within the stream by the number of bytes read.
 /// </summary>
 public override int Read(byte[] buffer, int offset, int count)
 {
     return(GpgMeHelper.Read(Handle, buffer, offset, count));
 }
コード例 #5
0
ファイル: GpgContext.cs プロジェクト: joanbarros/Gpg.NET
 /// <summary>
 /// Searches for keys matching the given search parameters, returning the first matching key.
 /// </summary>
 /// <param name="pattern">One or more GPG key IDs to search for. The default value (null) matches all keys.</param>
 /// <param name="privateOnly">True if only GPG keys for which a private key is available should be returned, false otherwise.</param>
 /// <returns>Returns the first GPG key that matches the given search parameters, or null if there are no matches.</returns>
 public GpgKey FindKey(string pattern, bool privateOnly = false)
 {
     return(GpgMeHelper.FindKey(this, pattern, privateOnly));
 }
コード例 #6
0
ファイル: GpgContext.cs プロジェクト: joanbarros/Gpg.NET
 /// <summary>
 /// Searches for keys matching the given search parameters.
 /// </summary>
 /// <param name="pattern">One or more GPG key IDs to search for. The default value (null) matches all keys.</param>
 /// <param name="privateOnly">True if only GPG keys for which a private key is available should be returned, false otherwise.</param>
 /// <returns>A list of GPG keys matching the given search parameters.</returns>
 public IEnumerable <GpgKey> FindKeys(string pattern = null, bool privateOnly = false)
 {
     return(GpgMeHelper.FindKeys(this, pattern, privateOnly));
 }