121 $this->_privateKey = $privateKey;
122 $this->_domainName = $domainName;
123 $this->_signerIdentity =
'@'.$domainName;
124 $this->_selector = $selector;
136 public static function newInstance($privateKey, $domainName, $selector)
138 return new static($privateKey, $domainName, $selector);
149 $this->_hashHandler = null;
150 $this->_bodyCanonIgnoreStart = 2;
151 $this->_bodyCanonEmptyCounter = 0;
152 $this->_bodyCanonLastChar = null;
153 $this->_bodyCanonSpace =
false;
178 foreach ($this->_bound as $is) {
211 $this->_bound[] = $is;
229 foreach ($this->_bound as $k => $stream) {
230 if ($stream === $is) {
231 unset($this->_bound[$k]);
264 $this->_hashAlgorithm =
'rsa-sha1';
278 if ($canon ==
'nofws') {
279 $this->_canon =
'nofws';
281 $this->_canon =
'simple';
296 $this->_signerIdentity = $identity;
310 $this->_debugHeaders = (bool) $debug;
337 if ($this->_debugHeaders) {
338 return array(
'DomainKey-Signature',
'X-DebugHash');
340 return array(
'DomainKey-Signature');
353 $this->_ignoredHeaders[strtolower($header_name)] =
true;
368 $this->_canonData =
'';
370 $listHeaders = $headers->
listAll();
371 foreach ($listHeaders as $hName) {
373 if (!isset($this->_ignoredHeaders[strtolower($hName)])) {
374 if ($headers->
has($hName)) {
375 $tmp = $headers->
getAll($hName);
376 foreach ($tmp as $header) {
377 if ($header->getFieldBody() !=
'') {
379 $this->_signedHeaders[] = $header->getFieldName();
400 $params = array(
'a' => $this->_hashAlgorithm,
'b' => chunk_split(base64_encode($this->
_getEncryptedHash()), 73,
' '),
'c' => $this->_canon,
'd' => $this->_domainName,
'h' => implode(
': ', $this->_signedHeaders),
'q' =>
'dns',
's' => $this->_selector);
402 foreach ($params as $k => $v) {
403 $string .= $k.
'='.$v.
'; ';
405 $string = trim($string);
415 switch ($this->_canon) {
418 $exploded = explode(
':', $header, 2);
419 $name = strtolower(trim($exploded[0]));
420 $value = str_replace(
"\r\n",
'', $exploded[1]);
421 $value = preg_replace(
"/[ \t][ \t]+/",
' ', $value);
422 $header = $name.
':'.trim($value).
"\r\n";
431 $this->_bodyCanonEmptyCounter = 1;
436 $len = strlen($string);
438 $nofws = ($this->_canon ==
'nofws');
439 for ($i = 0; $i < $len; ++$i) {
440 if ($this->_bodyCanonIgnoreStart > 0) {
444 switch ($string[$i]) {
446 $this->_bodyCanonLastChar =
"\r";
449 if ($this->_bodyCanonLastChar ==
"\r") {
451 $this->_bodyCanonSpace =
false;
453 if ($this->_bodyCanonLine ==
'') {
456 $this->_bodyCanonLine =
'';
461 throw new Swift_SwiftException(
'Invalid new line sequence in mail found \n without preceding \r');
468 $this->_bodyCanonSpace =
true;
472 if ($this->_bodyCanonEmptyCounter > 0) {
473 $canon .= str_repeat(
"\r\n", $this->_bodyCanonEmptyCounter);
474 $this->_bodyCanonEmptyCounter = 0;
476 $this->_bodyCanonLine .= $string[$i];
477 $canon .= $string[$i];
485 if (strlen($this->_bodyCanonLine) > 0) {
488 $this->_hash = hash_final($this->_hashHandler,
true);
493 $this->_canonData .= $string;
494 hash_update($this->_hashHandler, $string);
500 switch ($this->_hashAlgorithm) {
502 $this->_hashHandler = hash_init(
'sha1');
505 $this->_canonLine =
'';
516 $pkeyId = openssl_get_privatekey($this->_privateKey);
518 throw new Swift_SwiftException(
'Unable to load DomainKey Private Key ['.openssl_error_string().
']');
520 if (openssl_sign($this->_canonData, $signature, $pkeyId, OPENSSL_ALGO_SHA1)) {