| 887 | |
| 888 | |
| 889 | class PrivKeyV4(PrivKey, PubKeyV4): |
| 890 | __ver__ = 4 |
| 891 | |
| 892 | @classmethod |
| 893 | def new(cls, key_algorithm, key_size, created=None): |
| 894 | # build a key packet |
| 895 | pk = PrivKeyV4() |
| 896 | pk.pkalg = key_algorithm |
| 897 | if pk.keymaterial is None: |
| 898 | raise NotImplementedError(key_algorithm) |
| 899 | pk.keymaterial._generate(key_size) |
| 900 | if created is not None: |
| 901 | pk.created = created |
| 902 | pk.update_hlen() |
| 903 | return pk |
| 904 | |
| 905 | def pubkey(self): |
| 906 | # return a copy of ourselves, but just the public half |
| 907 | pk = PubKeyV4() if not isinstance(self, PrivSubKeyV4) else PubSubKeyV4() |
| 908 | pk.created = self.created |
| 909 | pk.pkalg = self.pkalg |
| 910 | |
| 911 | # copy over MPIs |
| 912 | for pm in self.keymaterial.__pubfields__: |
| 913 | setattr(pk.keymaterial, pm, copy.copy(getattr(self.keymaterial, pm))) |
| 914 | |
| 915 | if self.pkalg in {PubKeyAlgorithm.ECDSA, PubKeyAlgorithm.EdDSA}: |
| 916 | pk.keymaterial.oid = self.keymaterial.oid |
| 917 | |
| 918 | if self.pkalg == PubKeyAlgorithm.ECDH: |
| 919 | pk.keymaterial.oid = self.keymaterial.oid |
| 920 | pk.keymaterial.kdf = copy.copy(self.keymaterial.kdf) |
| 921 | |
| 922 | pk.update_hlen() |
| 923 | return pk |
| 924 | |
| 925 | @property |
| 926 | def protected(self): |
| 927 | return bool(self.keymaterial.s2k) |
| 928 | |
| 929 | @property |
| 930 | def unlocked(self): |
| 931 | if self.protected: |
| 932 | return 0 not in list(self.keymaterial) |
| 933 | return True # pragma: no cover |
| 934 | |
| 935 | def protect(self, passphrase, enc_alg, hash_alg): |
| 936 | self.keymaterial.encrypt_keyblob(passphrase, enc_alg, hash_alg) |
| 937 | del passphrase |
| 938 | self.update_hlen() |
| 939 | |
| 940 | def unprotect(self, passphrase): |
| 941 | self.keymaterial.decrypt_keyblob(passphrase) |
| 942 | del passphrase |
| 943 | |
| 944 | def sign(self, sigdata, hash_alg): |
| 945 | return self.keymaterial.sign(sigdata, hash_alg) |
| 946 |
no outgoing calls
no test coverage detected
searching dependent graphs…