Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support protection of worksheet by a specific hash algorithm #1485

Closed
wants to merge 2 commits into from

Conversation

reijnnn
Copy link
Contributor

@reijnnn reijnnn commented May 23, 2020

This is:

- [ ] a bugfix
- [x] a new feature

Checklist:

Why this change is needed?

This feature allows you to protect the worksheet using a specific hash algorithm (for example, md5, sha-256) and allows you to check the correct password when you open a document

More info

ISO Write Protection Method

Example write

<?php
require __DIR__ . '/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Shared\PasswordHasher;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$password = 'PhpSpreadsheet';
$salt = PasswordHasher::generateSalt();
$spinCount = 10000;

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getProtection()->setSheet(true);
$worksheet->getProtection()->setHashValue(PasswordHasher::hashPassword($password, PasswordHasher::ALGORITHM_SHA_256, $salt, $spinCount));
$worksheet->getProtection()->setAlgorithmName(PasswordHasher::ALGORITHM_SHA_256);
$worksheet->getProtection()->setSaltValue($salt);
$worksheet->getProtection()->setSpinCount($spinCount);

$writer = new Xlsx($spreadsheet);
$writer->save('tst_write_example.xlsx');

Example read

<?php
require __DIR__ . '/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Shared\PasswordHasher;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;

$reader = new Xlsx();
$spreadsheet = $reader->load('tst_write_example.xlsx');
$worksheet = $spreadsheet->getActiveSheet();

$password = 'PhpSpreadsheet';

if($worksheet->getProtection()->isProtectionEnabled()) {
   if($worksheet->getProtection()->getPassword()) {
      $hashPassword = $worksheet->getProtection()->getPassword();
      if($hashPassword == PasswordHasher::hashPassword($password)) {
         echo "[1] password is correct";
      } else {
         echo "[1] password is incorrect";
      }
   }

   if($worksheet->getProtection()->getHashValue()) {
      $hashPassword = $worksheet->getProtection()->getHashValue();
      $algorithmName = $worksheet->getProtection()->getAlgorithmName();
      $saltValue = $worksheet->getProtection()->getSaltValue();
      $spinCount = $worksheet->getProtection()->getSpinCount();
      if($hashPassword == PasswordHasher::hashPassword($password, $algorithmName, $saltValue, $spinCount)) {
         echo "[2] password is correct";
      } else {
         echo "[2] password is incorrect";
      }
   }
} else {
   echo "Protection is disabled";
}

@PowerKiKi
Copy link
Member

Merged with modification in b9a5966

@PowerKiKi PowerKiKi closed this May 31, 2020
PowerKiKi added a commit that referenced this pull request May 31, 2020
### Added

- Support writing to streams in all writers [#1292](#1292)
- Support CSV files with data wrapping a lot of lines [#1468](#1468)
- Support protection of worksheet by a specific hash algorithm [#1485](#1485)

### Fixed

- Fix Chart samples by updating chart parameter from 0 to DataSeries::EMPTY_AS_GAP [#1448](#1448)
- Fix return type in docblock for the Cells::get() [#1398](#1398)
- Fix RATE, PRICE, XIRR, and XNPV Functions [#1456](#1456)
- Save Excel 2010+ functions properly in XLSX [#1461](#1461)
- Several improvements in HTML writer [#1464](#1464)
- Fix incorrect behaviour when saving XLSX file with drawings [#1462](#1462),
- Fix Crash while trying setting a cell the value "123456\n" [#1476](#1481)
- Improved DATEDIF() function and reduced errors for Y and YM units [#1466](#1466)
- Stricter typing for mergeCells [#1494](#1494)

### Changed

- Drop support for PHP 7.1, according to https://phpspreadsheet.readthedocs.io/en/latest/#php-version-support
- Drop partial migration tool in favor of complete migration via RectorPHP [#1445](#1445)
- Limit composer package to `src/` [#1424](#1424)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants