-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Feature/Sheet Protection #907
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
Merged
guyonroche
merged 9 commits into
exceljs:master
from
karabaesh:feature/sheet-protection
Aug 15, 2019
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c6a97d0
New feature: Sheet protection
karabaesh bbb32f1
Merge branch 'master' into feature/sheet-protection
karabaesh f6468be
Additional unit tests
karabaesh 9ccdf54
Merge branch 'feature/sheet-protection' of https://github.com/karabae…
karabaesh 1d9c481
Fix sheet protection options
karabaesh 3f02ae4
Fix unit test 'encryptor'
karabaesh cdc0afd
Merge branch 'master' into feature/sheet-protection
karabaesh fdf201d
Included requests of guyonroche; Wrapped sheet protection into promise
karabaesh f575957
Changes requested by guyonroche concerning the hasing algorithm
karabaesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const crypto = require('crypto'); | ||
|
||
const Encryptor = { | ||
/** | ||
* Calculate a hash of the concatenated buffers with the given algorithm. | ||
* @param {string} algorithm - The hash algorithm. | ||
* @returns {Buffer} The hash | ||
*/ | ||
hash(algorithm, ...buffers) { | ||
const hashes = crypto.getHashes(); | ||
if (hashes.indexOf(algorithm) < 0) { | ||
throw new Error(`Hash algorithm '${ algorithm }' not supported!`); | ||
} | ||
const hash = crypto.createHash(algorithm); | ||
hash.update(Buffer.concat(buffers)); | ||
return hash.digest(); | ||
}, | ||
/** | ||
* Convert a password into an encryption key | ||
* @param {string} password - The password | ||
* @param {string} hashAlgorithm - The hash algoritm | ||
* @param {string} saltValue - The salt value | ||
* @param {number} spinCount - The spin count | ||
* @param {number} keyBits - The length of the key in bits | ||
* @param {Buffer} blockKey - The block key | ||
* @returns {Buffer} The encryption key | ||
*/ | ||
convertPasswordToHash(password, hashAlgorithm, saltValue, spinCount) { | ||
hashAlgorithm = hashAlgorithm.toLowerCase(); | ||
// Password must be in unicode buffer | ||
const passwordBuffer = Buffer.from(password, 'utf16le'); | ||
// Generate the initial hash | ||
let key = this.hash( | ||
hashAlgorithm, | ||
Buffer.from(saltValue, 'base64'), | ||
passwordBuffer | ||
); | ||
// Now regenerate until spin count | ||
for (let i = 0; i < spinCount; i++) { | ||
const iterator = Buffer.alloc(4); | ||
iterator.writeUInt32LE(i, 0); | ||
key = this.hash(hashAlgorithm, key, iterator); | ||
} | ||
return key.toString('base64'); | ||
}, | ||
/** | ||
* Generates cryptographically strong pseudo-random data. | ||
* @param size The size argument is a number indicating the number of bytes to generate. | ||
*/ | ||
randomBytes(size) { | ||
return crypto.randomBytes(size); | ||
}, | ||
}; | ||
module.exports = Encryptor; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const _ = require('../../../utils/under-dash'); | ||
const BaseXform = require('../base-xform'); | ||
|
||
function booleanToXml(model, value) { | ||
return model ? value : undefined; | ||
} | ||
|
||
function xmlToBoolean(value, equals) { | ||
return value === equals ? true : undefined; | ||
} | ||
|
||
class SheetProtectionXform extends BaseXform { | ||
get tag() { | ||
return 'sheetProtection'; | ||
} | ||
|
||
render(xmlStream, model) { | ||
if (model) { | ||
const attributes = { | ||
sheet: booleanToXml(model.sheet, '1'), | ||
selectLockedCells: model.selectLockedCells === false ? '1' : undefined, | ||
selectUnlockedCells: model.selectUnlockedCells === false ? '1' : undefined, | ||
formatCells: booleanToXml(model.formatCells, '0'), | ||
formatColumns: booleanToXml(model.formatColumns, '0'), | ||
formatRows: booleanToXml(model.formatRows, '0'), | ||
insertColumns: booleanToXml(model.insertColumns, '0'), | ||
insertRows: booleanToXml(model.insertRows, '0'), | ||
insertHyperlinks: booleanToXml(model.insertHyperlinks, '0'), | ||
deleteColumns: booleanToXml(model.deleteColumns, '0'), | ||
deleteRows: booleanToXml(model.deleteRows, '0'), | ||
sort: booleanToXml(model.sort, '0'), | ||
autoFilter: booleanToXml(model.autoFilter, '0'), | ||
pivotTables: booleanToXml(model.pivotTables, '0'), | ||
}; | ||
if(model.sheet){ | ||
attributes.algorithmName = model.algorithmName; | ||
attributes.hashValue = model.hashValue; | ||
attributes.saltValue = model.saltValue; | ||
attributes.spinCount = model.spinCount; | ||
attributes.objects = booleanToXml(model.objects === false, '1'); | ||
attributes.scenarios = booleanToXml(model.scenarios === false, '1'); | ||
} | ||
if (_.some(attributes, value => value !== undefined)) { | ||
xmlStream.leafNode(this.tag, attributes); | ||
} | ||
} | ||
} | ||
|
||
parseOpen(node) { | ||
switch (node.name) { | ||
case this.tag: | ||
this.model = { | ||
sheet: xmlToBoolean(node.attributes.sheet, '1'), | ||
objects: (node.attributes.objects === '1') ? false : undefined, | ||
scenarios: (node.attributes.scenarios === '1') ? false : undefined, | ||
selectLockedCells: (node.attributes.selectLockedCells === '1') ? false : undefined, | ||
selectUnlockedCells: (node.attributes.selectUnlockedCells === '1') ? false : undefined, | ||
formatCells: xmlToBoolean(node.attributes.formatCells, '0'), | ||
formatColumns: xmlToBoolean(node.attributes.formatColumns, '0'), | ||
formatRows: xmlToBoolean(node.attributes.formatRows, '0'), | ||
insertColumns: xmlToBoolean(node.attributes.insertColumns, '0'), | ||
insertRows: xmlToBoolean(node.attributes.insertRows, '0'), | ||
insertHyperlinks: xmlToBoolean(node.attributes.insertHyperlinks, '0'), | ||
deleteColumns: xmlToBoolean(node.attributes.deleteColumns, '0'), | ||
deleteRows: xmlToBoolean(node.attributes.deleteRows, '0'), | ||
sort: xmlToBoolean(node.attributes.sort, '0'), | ||
autoFilter: xmlToBoolean(node.attributes.autoFilter, '0'), | ||
pivotTables: xmlToBoolean(node.attributes.pivotTables, '0'), | ||
}; | ||
if(node.attributes.algorithmName){ | ||
this.model.algorithmName = node.attributes.algorithmName; | ||
this.model.hashValue = node.attributes.hashValue; | ||
this.model.saltValue = node.attributes.saltValue; | ||
this.model.spinCount = parseInt(node.attributes.spinCount, 10); | ||
} | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
parseText() {} | ||
|
||
parseClose() { | ||
return false; | ||
} | ||
} | ||
|
||
module.exports = SheetProtectionXform; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const { expect } = require('chai'); | ||
|
||
const Encryptor = require('../../../lib/utils/encryptor'); | ||
|
||
describe('Encryptor', () => { | ||
it('Generates SHA-512 hash for given password, salt value and spin count', () => { | ||
const password = '123'; | ||
const saltValue = '6tC6yotbNa8JaMaDvbUgxw=='; | ||
const spinCount = 100000; | ||
const hash = Encryptor.convertPasswordToHash(password, 'SHA512', saltValue, spinCount); | ||
expect(hash).to.equal('RHtx1KpAYT7nBzGCTInkHrbf2wTZxP3BT4Eo8PBHPTM4KfKArJTluFvizDvo6GnBCOO6JJu7qwKvMqnKHs7dcw=='); | ||
}); | ||
|
||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.