2

I am generating excel files with extension .xls using PHPExcel library. The excel file is generating. I am using password protection for the document and made only some fields editable. I am doing an Export Import mechanism. It is perfectly working now and I need to add some modification.

My question is

Is it possible to verify the password I given to protect the document? So that I can check it at the time of import

For example

If I protect the document using

$sheet -> getProtection() -> setPassword('MyPassword');

Is there any function like below for checking the password?

$newsheet -> getProtection() -> verifyPassword('MyPassword');

Any help could be appreciated.

Arun
  • 3,640
  • 7
  • 44
  • 87
  • I'm not well versed in PHPExcel, but could you not try to load it in PHPExcel again separately and attempt an edit with the password? – SArnab Jul 05 '16 at 12:09
  • @SArnab, Thank you for the consideration. But I don't want to edit it. I just need a verification of the password. – Arun Jul 05 '16 at 12:11
  • What do you mean by “validation”? Your question reads if you just wanted to check that the `setPassword` method actually works? If so - why? Do you have any reason to doubt it? – CBroe Jul 05 '16 at 12:13
  • @CBroe, Sorry for the typo. I need a verification method. I can download a protected excel and upload it via another interface. For ensure that it is the same file, I just need to verify the password. – Arun Jul 05 '16 at 12:15

1 Answers1

5

You should be able to use

$hash = $sheet->getProtection()->getPassword(); // returns a hash
$valid = ($hash === PHPExcel_Shared_PasswordHasher::hashPassword($password));

if($valid) {
    //
}
Tom Regner
  • 6,856
  • 4
  • 32
  • 47