| @@ 94-147 (lines=54) @@ | ||
| 91 | * @throws Error |
|
| 92 | * @throws TypeError |
|
| 93 | */ |
|
| 94 | public static function box_open($inputFile, $outputFile, $nonce, $keypair) |
|
| 95 | { |
|
| 96 | /* Type checks: */ |
|
| 97 | if (!is_string($inputFile)) { |
|
| 98 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 99 | } |
|
| 100 | if (!is_string($outputFile)) { |
|
| 101 | throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 102 | } |
|
| 103 | if (!is_string($nonce)) { |
|
| 104 | throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 105 | } |
|
| 106 | if (!is_string($keypair)) { |
|
| 107 | throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.'); |
|
| 108 | } |
|
| 109 | ||
| 110 | /* Input validation: */ |
|
| 111 | if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
| 112 | throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
| 113 | } |
|
| 114 | if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 115 | throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 116 | } |
|
| 117 | ||
| 118 | /** @var int $size */ |
|
| 119 | $size = filesize($inputFile); |
|
| 120 | if (!is_int($size)) { |
|
| 121 | throw new Error('Could not obtain the file size'); |
|
| 122 | } |
|
| 123 | ||
| 124 | /** @var resource $ifp */ |
|
| 125 | $ifp = fopen($inputFile, 'rb'); |
|
| 126 | if (!is_resource($ifp)) { |
|
| 127 | throw new Error('Could not open input file for reading'); |
|
| 128 | } |
|
| 129 | ||
| 130 | /** @var resource $ofp */ |
|
| 131 | $ofp = fopen($outputFile, 'wb'); |
|
| 132 | if (!is_resource($ofp)) { |
|
| 133 | fclose($ifp); |
|
| 134 | throw new Error('Could not open output file for writing'); |
|
| 135 | } |
|
| 136 | ||
| 137 | $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair); |
|
| 138 | fclose($ifp); |
|
| 139 | fclose($ofp); |
|
| 140 | try { |
|
| 141 | ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 142 | ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 143 | } catch (Error $ex) { |
|
| 144 | unset($ephKeypair); |
|
| 145 | } |
|
| 146 | return $res; |
|
| 147 | } |
|
| 148 | ||
| 149 | /** |
|
| 150 | * Seal a file (rather than a string). Uses less memory than |
|
| @@ 491-543 (lines=53) @@ | ||
| 488 | * @throws Error |
|
| 489 | * @throws TypeError |
|
| 490 | */ |
|
| 491 | public static function secretbox_open($inputFile, $outputFile, $nonce, $key) |
|
| 492 | { |
|
| 493 | /* Type checks: */ |
|
| 494 | if (!is_string($inputFile)) { |
|
| 495 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 496 | } |
|
| 497 | if (!is_string($outputFile)) { |
|
| 498 | throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 499 | } |
|
| 500 | if (!is_string($nonce)) { |
|
| 501 | throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 502 | } |
|
| 503 | if (!is_string($key)) { |
|
| 504 | throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
| 505 | } |
|
| 506 | ||
| 507 | /* Input validation: */ |
|
| 508 | if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
| 509 | throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
| 510 | } |
|
| 511 | if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 512 | throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes'); |
|
| 513 | } |
|
| 514 | ||
| 515 | /** @var int $size */ |
|
| 516 | $size = filesize($inputFile); |
|
| 517 | if (!is_int($size)) { |
|
| 518 | throw new Error('Could not obtain the file size'); |
|
| 519 | } |
|
| 520 | ||
| 521 | /** @var resource $ifp */ |
|
| 522 | $ifp = fopen($inputFile, 'rb'); |
|
| 523 | if (!is_resource($ifp)) { |
|
| 524 | throw new Error('Could not open input file for reading'); |
|
| 525 | } |
|
| 526 | ||
| 527 | /** @var resource $ofp */ |
|
| 528 | $ofp = fopen($outputFile, 'wb'); |
|
| 529 | if (!is_resource($ofp)) { |
|
| 530 | fclose($ifp); |
|
| 531 | throw new Error('Could not open output file for writing'); |
|
| 532 | } |
|
| 533 | ||
| 534 | $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key); |
|
| 535 | fclose($ifp); |
|
| 536 | fclose($ofp); |
|
| 537 | try { |
|
| 538 | ParagonIE_Sodium_Compat::memzero($key); |
|
| 539 | } catch (Error $ex) { |
|
| 540 | unset($key); |
|
| 541 | } |
|
| 542 | return $res; |
|
| 543 | } |
|
| 544 | ||
| 545 | /** |
|
| 546 | * Sign a file (rather than a string). Uses less memory than |
|