Keyczar.MutableKeySet.PublicKey C# (CSharp) Method

PublicKey() public method

Returns keyset with only the public keys.
public PublicKey ( ) : MutableKeySet
return MutableKeySet
        public MutableKeySet PublicKey()
        {
            if (!typeof (IPrivateKey).IsAssignableFrom(Metadata.KeyType.RepresentedType))
            {
                return null;
            }

            var newMeta = new KeyMetadata(Metadata);
            newMeta.Purpose = newMeta.Purpose == KeyPurpose.SignAndVerify
                                  ? KeyPurpose.Verify
                                  : KeyPurpose.Encrypt;

            var copiedKeys = _keys.Select(p => new {p.Key, ((IPrivateKey) p.Value).PublicKey})
                                  .Select(
                                      p =>
                                      new
                                          {
                                              p.Key,
                                              Type = p.PublicKey.KeyType,
                                              Value = Keyczar.RawStringEncoding.GetBytes(p.PublicKey.ToJson())
                                          })
                                  .Select(p => new {p.Key, Value = Key.Read(p.Type, p.Value)});

            newMeta.KeyType = copiedKeys.Select(it => it.Value.KeyType).First();

            return new MutableKeySet(newMeta, copiedKeys.ToDictionary(k => k.Key, v => v.Value));
        }

Usage Example

Beispiel #1
0
        public override int Run(string[] remainingArguments)
        {
            var ret = 0;
            Crypter crypter = null;
            IKeySet ks = new KeySet(_location);

            Func<string> prompt = CachedPrompt.Password(Util.PromptForPassword).Prompt;

            IDisposable dks = null;
            if (!String.IsNullOrWhiteSpace(_crypterLocation))
            {
                if (_password)
                {
                    var cks = new PbeKeySet(_crypterLocation, prompt);
                    crypter = new Crypter(cks);
                    dks = cks;
                }
                else
                {
                    crypter = new Crypter(_crypterLocation);
                }
                ks = new EncryptedKeySet(ks, crypter);
            }
            else if (_password)
            {
                ks = new PbeKeySet(ks, prompt);
            }
            var d2ks = ks as IDisposable;

            using (crypter)
            using (dks)
            using (d2ks)
            using (var keySet = new MutableKeySet(ks))
            {
                var pubKeySet = keySet.PublicKey();
                if (pubKeySet != null)
                {
                    using (pubKeySet)
                    {
                        IKeySetWriter writer = new KeySetWriter(_destination, overwrite: false);

                        if (pubKeySet.Save(writer))
                        {
                            Console.WriteLine(Localized.MsgNewPublicKeySet);
                            ret = 0;
                        }
                        else
                        {
                            ret = -1;
                        }
                    }
                }
                else
                {
                    ret = -1;
                }
            }

            return ret;
        }