PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/mail/plugins/enigma/lib/Crypt/GPG/Key.php

https://bitbucket.org/alisher/itworks
PHP | 223 lines | 57 code | 25 blank | 141 comment | 3 complexity | 0b99f9074e96a2388c2bf064be0c02d5 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Contains a class representing GPG keys
  5. *
  6. * PHP version 5
  7. *
  8. * LICENSE:
  9. *
  10. * This library is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as
  12. * published by the Free Software Foundation; either version 2.1 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * @category Encryption
  25. * @package Crypt_GPG
  26. * @author Michael Gauthier <mike@silverorange.com>
  27. * @copyright 2008-2010 silverorange
  28. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  29. * @version CVS: $Id: Key.php 295621 2010-03-01 04:18:54Z gauthierm $
  30. * @link http://pear.php.net/package/Crypt_GPG
  31. */
  32. /**
  33. * Sub-key class definition
  34. */
  35. require_once 'Crypt/GPG/SubKey.php';
  36. /**
  37. * User id class definition
  38. */
  39. require_once 'Crypt/GPG/UserId.php';
  40. // {{{ class Crypt_GPG_Key
  41. /**
  42. * A data class for GPG key information
  43. *
  44. * This class is used to store the results of the {@link Crypt_GPG::getKeys()}
  45. * method.
  46. *
  47. * @category Encryption
  48. * @package Crypt_GPG
  49. * @author Michael Gauthier <mike@silverorange.com>
  50. * @copyright 2008-2010 silverorange
  51. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  52. * @link http://pear.php.net/package/Crypt_GPG
  53. * @see Crypt_GPG::getKeys()
  54. */
  55. class Crypt_GPG_Key
  56. {
  57. // {{{ class properties
  58. /**
  59. * The user ids associated with this key
  60. *
  61. * This is an array of {@link Crypt_GPG_UserId} objects.
  62. *
  63. * @var array
  64. *
  65. * @see Crypt_GPG_Key::addUserId()
  66. * @see Crypt_GPG_Key::getUserIds()
  67. */
  68. private $_userIds = array();
  69. /**
  70. * The subkeys of this key
  71. *
  72. * This is an array of {@link Crypt_GPG_SubKey} objects.
  73. *
  74. * @var array
  75. *
  76. * @see Crypt_GPG_Key::addSubKey()
  77. * @see Crypt_GPG_Key::getSubKeys()
  78. */
  79. private $_subKeys = array();
  80. // }}}
  81. // {{{ getSubKeys()
  82. /**
  83. * Gets the sub-keys of this key
  84. *
  85. * @return array the sub-keys of this key.
  86. *
  87. * @see Crypt_GPG_Key::addSubKey()
  88. */
  89. public function getSubKeys()
  90. {
  91. return $this->_subKeys;
  92. }
  93. // }}}
  94. // {{{ getUserIds()
  95. /**
  96. * Gets the user ids of this key
  97. *
  98. * @return array the user ids of this key.
  99. *
  100. * @see Crypt_GPG_Key::addUserId()
  101. */
  102. public function getUserIds()
  103. {
  104. return $this->_userIds;
  105. }
  106. // }}}
  107. // {{{ getPrimaryKey()
  108. /**
  109. * Gets the primary sub-key of this key
  110. *
  111. * The primary key is the first added sub-key.
  112. *
  113. * @return Crypt_GPG_SubKey the primary sub-key of this key.
  114. */
  115. public function getPrimaryKey()
  116. {
  117. $primary_key = null;
  118. if (count($this->_subKeys) > 0) {
  119. $primary_key = $this->_subKeys[0];
  120. }
  121. return $primary_key;
  122. }
  123. // }}}
  124. // {{{ canSign()
  125. /**
  126. * Gets whether or not this key can sign data
  127. *
  128. * This key can sign data if any sub-key of this key can sign data.
  129. *
  130. * @return boolean true if this key can sign data and false if this key
  131. * cannot sign data.
  132. */
  133. public function canSign()
  134. {
  135. $canSign = false;
  136. foreach ($this->_subKeys as $subKey) {
  137. if ($subKey->canSign()) {
  138. $canSign = true;
  139. break;
  140. }
  141. }
  142. return $canSign;
  143. }
  144. // }}}
  145. // {{{ canEncrypt()
  146. /**
  147. * Gets whether or not this key can encrypt data
  148. *
  149. * This key can encrypt data if any sub-key of this key can encrypt data.
  150. *
  151. * @return boolean true if this key can encrypt data and false if this
  152. * key cannot encrypt data.
  153. */
  154. public function canEncrypt()
  155. {
  156. $canEncrypt = false;
  157. foreach ($this->_subKeys as $subKey) {
  158. if ($subKey->canEncrypt()) {
  159. $canEncrypt = true;
  160. break;
  161. }
  162. }
  163. return $canEncrypt;
  164. }
  165. // }}}
  166. // {{{ addSubKey()
  167. /**
  168. * Adds a sub-key to this key
  169. *
  170. * The first added sub-key will be the primary key of this key.
  171. *
  172. * @param Crypt_GPG_SubKey $subKey the sub-key to add.
  173. *
  174. * @return Crypt_GPG_Key the current object, for fluent interface.
  175. */
  176. public function addSubKey(Crypt_GPG_SubKey $subKey)
  177. {
  178. $this->_subKeys[] = $subKey;
  179. return $this;
  180. }
  181. // }}}
  182. // {{{ addUserId()
  183. /**
  184. * Adds a user id to this key
  185. *
  186. * @param Crypt_GPG_UserId $userId the user id to add.
  187. *
  188. * @return Crypt_GPG_Key the current object, for fluent interface.
  189. */
  190. public function addUserId(Crypt_GPG_UserId $userId)
  191. {
  192. $this->_userIds[] = $userId;
  193. return $this;
  194. }
  195. // }}}
  196. }
  197. // }}}
  198. ?>