forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spreadsheet: add support for protecting/unprotecting workbooks/sheets
- Loading branch information
Showing
6 changed files
with
223 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,29 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
// | ||
// Use of this source code is governed by the terms of the Affero GNU General | ||
// Public License version 3.0 as published by the Free Software Foundation and | ||
// appearing in the file LICENSE included in the packaging of this file. A | ||
// commercial license can be purchased by contacting [email protected]. | ||
|
||
package spreadsheet | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// PasswordHash returns the password hash for a workbook using the modified | ||
// spreadsheetML password hash that is compatible with Excel. | ||
func PasswordHash(s string) string { | ||
hash := uint16(0) | ||
if len(s) > 0 { | ||
for i := len(s) - 1; i >= 0; i-- { | ||
c := s[i] | ||
hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff) | ||
hash ^= uint16(c) | ||
} | ||
hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff) | ||
hash ^= uint16(len(s)) | ||
hash ^= (0x8000 | ('N' << 8) | 'K') | ||
} | ||
return fmt.Sprintf("%04X", uint64(hash)) | ||
} |
This file contains 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,29 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
// | ||
// Use of this source code is governed by the terms of the Affero GNU General | ||
// Public License version 3.0 as published by the Free Software Foundation and | ||
// appearing in the file LICENSE included in the packaging of this file. A | ||
// commercial license can be purchased by contacting [email protected]. | ||
|
||
package spreadsheet_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"baliance.com/gooxml/spreadsheet" | ||
) | ||
|
||
func TestKnownHashes(t *testing.T) { | ||
td := []struct { | ||
Inp string | ||
Exp string | ||
}{ | ||
{"gooxml", "DD67"}, | ||
{"", "0000"}, | ||
} | ||
for _, tc := range td { | ||
if got := spreadsheet.PasswordHash(tc.Inp); got != tc.Exp { | ||
t.Errorf("expected hash of %s = %s, got %s", tc.Inp, tc.Exp, got) | ||
} | ||
} | ||
} |
This file contains 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 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,68 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
// | ||
// Use of this source code is governed by the terms of the Affero GNU General | ||
// Public License version 3.0 as published by the Free Software Foundation and | ||
// appearing in the file LICENSE included in the packaging of this file. A | ||
// commercial license can be purchased by contacting [email protected]. | ||
|
||
package spreadsheet | ||
|
||
import ( | ||
"baliance.com/gooxml" | ||
sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" | ||
) | ||
|
||
type SheetProtection struct { | ||
x *sml.CT_SheetProtection | ||
} | ||
|
||
// X returns the inner wrapped XML type. | ||
func (p SheetProtection) X() *sml.CT_SheetProtection { | ||
return p.x | ||
} | ||
|
||
// IsSheetLocked returns whether the sheet is locked. | ||
func (p SheetProtection) IsSheetLocked() bool { | ||
return p.x.SheetAttr != nil && *p.x.SheetAttr | ||
} | ||
|
||
// LockSheet controls the locking of the sheet. | ||
func (p SheetProtection) LockSheet(b bool) { | ||
if !b { | ||
p.x.SheetAttr = nil | ||
} else { | ||
p.x.SheetAttr = gooxml.Bool(true) | ||
} | ||
} | ||
|
||
// IsSheetLocked returns whether the sheet objects are locked. | ||
func (p SheetProtection) IsObjectLocked() bool { | ||
return p.x.ObjectsAttr != nil && *p.x.ObjectsAttr | ||
} | ||
|
||
// LockObject controls the locking of the sheet objects. | ||
func (p SheetProtection) LockObject(b bool) { | ||
if !b { | ||
p.x.ObjectsAttr = nil | ||
} else { | ||
p.x.ObjectsAttr = gooxml.Bool(true) | ||
} | ||
} | ||
|
||
// PasswordHash returns the hash of the workbook password. | ||
func (p SheetProtection) PasswordHash() string { | ||
if p.x.PasswordAttr == nil { | ||
return "" | ||
} | ||
return *p.x.PasswordAttr | ||
} | ||
|
||
// SetPassword sets the password hash to a hash of the input password. | ||
func (p SheetProtection) SetPassword(pw string) { | ||
p.SetPasswordHash(PasswordHash(pw)) | ||
} | ||
|
||
// SetPasswordHash sets the password hash to the input. | ||
func (p SheetProtection) SetPasswordHash(pwHash string) { | ||
p.x.PasswordAttr = gooxml.String(pwHash) | ||
} |
This file contains 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 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,68 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
// | ||
// Use of this source code is governed by the terms of the Affero GNU General | ||
// Public License version 3.0 as published by the Free Software Foundation and | ||
// appearing in the file LICENSE included in the packaging of this file. A | ||
// commercial license can be purchased by contacting [email protected]. | ||
|
||
package spreadsheet | ||
|
||
import ( | ||
"baliance.com/gooxml" | ||
sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" | ||
) | ||
|
||
type WorkbookProtection struct { | ||
x *sml.CT_WorkbookProtection | ||
} | ||
|
||
// X returns the inner wrapped XML type. | ||
func (p WorkbookProtection) X() *sml.CT_WorkbookProtection { | ||
return p.x | ||
} | ||
|
||
// IsStructureLocked returns whether the workbook structure is locked. | ||
func (p WorkbookProtection) IsStructureLocked() bool { | ||
return p.x.LockStructureAttr != nil && *p.x.LockStructureAttr | ||
} | ||
|
||
// LockStructure controls the locking of the workbook structure. | ||
func (p WorkbookProtection) LockStructure(b bool) { | ||
if !b { | ||
p.x.LockStructureAttr = nil | ||
} else { | ||
p.x.LockStructureAttr = gooxml.Bool(true) | ||
} | ||
} | ||
|
||
// IsWindowLocked returns whether the workbook windows are locked. | ||
func (p WorkbookProtection) IsWindowLocked() bool { | ||
return p.x.LockWindowsAttr != nil && *p.x.LockWindowsAttr | ||
} | ||
|
||
// LockWindow controls the locking of the workbook windows. | ||
func (p WorkbookProtection) LockWindow(b bool) { | ||
if !b { | ||
p.x.LockWindowsAttr = nil | ||
} else { | ||
p.x.LockWindowsAttr = gooxml.Bool(true) | ||
} | ||
} | ||
|
||
// PasswordHash returns the hash of the workbook password. | ||
func (p WorkbookProtection) PasswordHash() string { | ||
if p.x.WorkbookPasswordAttr == nil { | ||
return "" | ||
} | ||
return *p.x.WorkbookPasswordAttr | ||
} | ||
|
||
// SetPassword sets the password hash to a hash of the input password. | ||
func (p WorkbookProtection) SetPassword(pw string) { | ||
p.SetPasswordHash(PasswordHash(pw)) | ||
} | ||
|
||
// SetPasswordHash sets the password hash to the input. | ||
func (p WorkbookProtection) SetPasswordHash(pwHash string) { | ||
p.x.WorkbookPasswordAttr = gooxml.String(pwHash) | ||
} |