This update docs and tests for workbook encryption
This commit is contained in:
parent
7a6d5f5ebe
commit
8fde918d98
|
@ -5,7 +5,7 @@ test/Test*.xlsx
|
||||||
test/Test*.xltm
|
test/Test*.xltm
|
||||||
test/Test*.xltx
|
test/Test*.xltx
|
||||||
# generated files
|
# generated files
|
||||||
test/BadEncrypt.xlsx
|
test/Encryption*.xlsx
|
||||||
test/BadWorkbook.SaveAsEmptyStruct.xlsx
|
test/BadWorkbook.SaveAsEmptyStruct.xlsx
|
||||||
test/*.png
|
test/*.png
|
||||||
test/excelize-*
|
test/excelize-*
|
||||||
|
|
17
crypt.go
17
crypt.go
|
@ -143,15 +143,10 @@ func Decrypt(raw []byte, opt *Options) (packageBuf []byte, err error) {
|
||||||
if err != nil || mechanism == "extensible" {
|
if err != nil || mechanism == "extensible" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch mechanism {
|
if mechanism == "agile" {
|
||||||
case "agile":
|
|
||||||
return agileDecrypt(encryptionInfoBuf, encryptedPackageBuf, opt)
|
return agileDecrypt(encryptionInfoBuf, encryptedPackageBuf, opt)
|
||||||
case "standard":
|
|
||||||
return standardDecrypt(encryptionInfoBuf, encryptedPackageBuf, opt)
|
|
||||||
default:
|
|
||||||
err = ErrUnsupportedEncryptMechanism
|
|
||||||
}
|
}
|
||||||
return
|
return standardDecrypt(encryptionInfoBuf, encryptedPackageBuf, opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encrypt API encrypt data with the password.
|
// Encrypt API encrypt data with the password.
|
||||||
|
@ -1112,7 +1107,7 @@ func (c *cfb) writeDirectoryEntry(propertyCount, customSectID, size int) []byte
|
||||||
return storage.stream
|
return storage.stream
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeMSAT provides a function to write compound file sector allocation
|
// writeMSAT provides a function to write compound file master sector allocation
|
||||||
// table.
|
// table.
|
||||||
func (c *cfb) writeMSAT(MSATBlocks, SATBlocks int, MSAT []int) []int {
|
func (c *cfb) writeMSAT(MSATBlocks, SATBlocks int, MSAT []int) []int {
|
||||||
if MSATBlocks > 0 {
|
if MSATBlocks > 0 {
|
||||||
|
@ -1129,7 +1124,8 @@ func (c *cfb) writeMSAT(MSATBlocks, SATBlocks int, MSAT []int) []int {
|
||||||
}
|
}
|
||||||
MSAT = append(MSAT, -1)
|
MSAT = append(MSAT, -1)
|
||||||
}
|
}
|
||||||
} else {
|
return MSAT
|
||||||
|
}
|
||||||
for i := 0; i < 109; i++ {
|
for i := 0; i < 109; i++ {
|
||||||
if i < SATBlocks {
|
if i < SATBlocks {
|
||||||
MSAT = append(MSAT, i)
|
MSAT = append(MSAT, i)
|
||||||
|
@ -1137,11 +1133,10 @@ func (c *cfb) writeMSAT(MSATBlocks, SATBlocks int, MSAT []int) []int {
|
||||||
}
|
}
|
||||||
MSAT = append(MSAT, -1)
|
MSAT = append(MSAT, -1)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return MSAT
|
return MSAT
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeSAT provides a function to write compound file master sector allocation
|
// writeSAT provides a function to write compound file sector allocation
|
||||||
// table.
|
// table.
|
||||||
func (c *cfb) writeSAT(MSATBlocks, SATBlocks, SSATBlocks, directoryBlocks, fileBlocks, streamBlocks int, SAT []int) (int, []int) {
|
func (c *cfb) writeSAT(MSATBlocks, SATBlocks, SSATBlocks, directoryBlocks, fileBlocks, streamBlocks int, SAT []int) (int, []int) {
|
||||||
var blocks int
|
var blocks int
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
package excelize
|
package excelize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -30,6 +31,13 @@ func TestEncrypt(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "SECRET", cell)
|
assert.Equal(t, "SECRET", cell)
|
||||||
assert.NoError(t, f.Close())
|
assert.NoError(t, f.Close())
|
||||||
|
// Test decrypt spreadsheet with unsupported encrypt mechanism
|
||||||
|
raw, err := ioutil.ReadFile(filepath.Join("test", "encryptAES.xlsx"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
raw[2050] = 3
|
||||||
|
_, err = Decrypt(raw, &Options{Password: "password"})
|
||||||
|
assert.EqualError(t, err, ErrUnsupportedEncryptMechanism.Error())
|
||||||
|
|
||||||
// Test encrypt spreadsheet with invalid password
|
// Test encrypt spreadsheet with invalid password
|
||||||
assert.EqualError(t, f.SaveAs(filepath.Join("test", "Encryption.xlsx"), Options{Password: strings.Repeat("*", MaxFieldLength+1)}), ErrPasswordLengthInvalid.Error())
|
assert.EqualError(t, f.SaveAs(filepath.Join("test", "Encryption.xlsx"), Options{Password: strings.Repeat("*", MaxFieldLength+1)}), ErrPasswordLengthInvalid.Error())
|
||||||
// Test encrypt spreadsheet with new password
|
// Test encrypt spreadsheet with new password
|
||||||
|
@ -51,6 +59,11 @@ func TestEncryptionMechanism(t *testing.T) {
|
||||||
assert.EqualError(t, err, ErrUnknownEncryptMechanism.Error())
|
assert.EqualError(t, err, ErrUnknownEncryptMechanism.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEncryptionWriteDirectoryEntry(t *testing.T) {
|
||||||
|
cfb := cfb{}
|
||||||
|
assert.Equal(t, 1536, len(cfb.writeDirectoryEntry(0, 0, -1)))
|
||||||
|
}
|
||||||
|
|
||||||
func TestHashing(t *testing.T) {
|
func TestHashing(t *testing.T) {
|
||||||
assert.Equal(t, hashing("unsupportedHashAlgorithm", []byte{}), []byte(nil))
|
assert.Equal(t, hashing("unsupportedHashAlgorithm", []byte{}), []byte(nil))
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,6 +93,7 @@ type Options struct {
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
// Close the file by Close function after opening the spreadsheet.
|
||||||
func OpenFile(filename string, opt ...Options) (*File, error) {
|
func OpenFile(filename string, opt ...Options) (*File, error) {
|
||||||
file, err := os.Open(filepath.Clean(filename))
|
file, err := os.Open(filepath.Clean(filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -129,6 +129,8 @@ func TestStreamWriter(t *testing.T) {
|
||||||
}
|
}
|
||||||
assert.NoError(t, rows.Close())
|
assert.NoError(t, rows.Close())
|
||||||
assert.Equal(t, 2559558, cells)
|
assert.Equal(t, 2559558, cells)
|
||||||
|
// Save spreadsheet with password.
|
||||||
|
assert.NoError(t, file.SaveAs(filepath.Join("test", "EncryptionTestStreamWriter.xlsx"), Options{Password: "password"}))
|
||||||
assert.NoError(t, file.Close())
|
assert.NoError(t, file.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue