Skip to content

Commit a0396ed

Browse files
committed
This update docs and tests for workbook encryption
1 parent d5f8618 commit a0396ed

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test/Test*.xlsx
55
test/Test*.xltm
66
test/Test*.xltx
77
# generated files
8-
test/BadEncrypt.xlsx
8+
test/Encryption*.xlsx
99
test/BadWorkbook.SaveAsEmptyStruct.xlsx
1010
test/*.png
1111
test/excelize-*

crypt.go

+11-16
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,10 @@ func Decrypt(raw []byte, opt *Options) (packageBuf []byte, err error) {
143143
if err != nil || mechanism == "extensible" {
144144
return
145145
}
146-
switch mechanism {
147-
case "agile":
146+
if mechanism == "agile" {
148147
return agileDecrypt(encryptionInfoBuf, encryptedPackageBuf, opt)
149-
case "standard":
150-
return standardDecrypt(encryptionInfoBuf, encryptedPackageBuf, opt)
151-
default:
152-
err = ErrUnsupportedEncryptMechanism
153148
}
154-
return
149+
return standardDecrypt(encryptionInfoBuf, encryptedPackageBuf, opt)
155150
}
156151

157152
// Encrypt API encrypt data with the password.
@@ -1112,7 +1107,7 @@ func (c *cfb) writeDirectoryEntry(propertyCount, customSectID, size int) []byte
11121107
return storage.stream
11131108
}
11141109

1115-
// writeMSAT provides a function to write compound file sector allocation
1110+
// writeMSAT provides a function to write compound file master sector allocation
11161111
// table.
11171112
func (c *cfb) writeMSAT(MSATBlocks, SATBlocks int, MSAT []int) []int {
11181113
if MSATBlocks > 0 {
@@ -1129,19 +1124,19 @@ func (c *cfb) writeMSAT(MSATBlocks, SATBlocks int, MSAT []int) []int {
11291124
}
11301125
MSAT = append(MSAT, -1)
11311126
}
1132-
} else {
1133-
for i := 0; i < 109; i++ {
1134-
if i < SATBlocks {
1135-
MSAT = append(MSAT, i)
1136-
continue
1137-
}
1138-
MSAT = append(MSAT, -1)
1127+
return MSAT
1128+
}
1129+
for i := 0; i < 109; i++ {
1130+
if i < SATBlocks {
1131+
MSAT = append(MSAT, i)
1132+
continue
11391133
}
1134+
MSAT = append(MSAT, -1)
11401135
}
11411136
return MSAT
11421137
}
11431138

1144-
// writeSAT provides a function to write compound file master sector allocation
1139+
// writeSAT provides a function to write compound file sector allocation
11451140
// table.
11461141
func (c *cfb) writeSAT(MSATBlocks, SATBlocks, SSATBlocks, directoryBlocks, fileBlocks, streamBlocks int, SAT []int) (int, []int) {
11471142
var blocks int

crypt_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package excelize
1313

1414
import (
15+
"io/ioutil"
1516
"path/filepath"
1617
"strings"
1718
"testing"
@@ -30,6 +31,13 @@ func TestEncrypt(t *testing.T) {
3031
assert.NoError(t, err)
3132
assert.Equal(t, "SECRET", cell)
3233
assert.NoError(t, f.Close())
34+
// Test decrypt spreadsheet with unsupported encrypt mechanism
35+
raw, err := ioutil.ReadFile(filepath.Join("test", "encryptAES.xlsx"))
36+
assert.NoError(t, err)
37+
raw[2050] = 3
38+
_, err = Decrypt(raw, &Options{Password: "password"})
39+
assert.EqualError(t, err, ErrUnsupportedEncryptMechanism.Error())
40+
3341
// Test encrypt spreadsheet with invalid password
3442
assert.EqualError(t, f.SaveAs(filepath.Join("test", "Encryption.xlsx"), Options{Password: strings.Repeat("*", MaxFieldLength+1)}), ErrPasswordLengthInvalid.Error())
3543
// Test encrypt spreadsheet with new password
@@ -51,6 +59,11 @@ func TestEncryptionMechanism(t *testing.T) {
5159
assert.EqualError(t, err, ErrUnknownEncryptMechanism.Error())
5260
}
5361

62+
func TestEncryptionWriteDirectoryEntry(t *testing.T) {
63+
cfb := cfb{}
64+
assert.Equal(t, 1536, len(cfb.writeDirectoryEntry(0, 0, -1)))
65+
}
66+
5467
func TestHashing(t *testing.T) {
5568
assert.Equal(t, hashing("unsupportedHashAlgorithm", []byte{}), []byte(nil))
5669
}

excelize.go

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ type Options struct {
9393
// return
9494
// }
9595
//
96+
// Close the file by Close function after opening the spreadsheet.
9697
func OpenFile(filename string, opt ...Options) (*File, error) {
9798
file, err := os.Open(filepath.Clean(filename))
9899
if err != nil {

stream_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ func TestStreamWriter(t *testing.T) {
129129
}
130130
assert.NoError(t, rows.Close())
131131
assert.Equal(t, 2559558, cells)
132+
// Save spreadsheet with password.
133+
assert.NoError(t, file.SaveAs(filepath.Join("test", "EncryptionTestStreamWriter.xlsx"), Options{Password: "password"}))
132134
assert.NoError(t, file.Close())
133135
}
134136

0 commit comments

Comments
 (0)