summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-05-20 14:28:22 +0900
committerRandy Morgan <[email protected]>2012-05-20 14:28:22 +0900
commita2ffc47d7589eb6e9d0cecf880ec1c84ddfd8316 (patch)
treed114f238ba6d9e015709b97c330237ed11eb5a67
parent0aede23fd2d8649bce223b36c28e5c77bf3749c4 (diff)
downloadcaxlsx-a2ffc47d7589eb6e9d0cecf880ec1c84ddfd8316.tar.gz
caxlsx-a2ffc47d7589eb6e9d0cecf880ec1c84ddfd8316.zip
more work on sheet protection. Protection is working, but specifying a password does not work. Having a bit of trouble implementing the hashing algorithm...
-rw-r--r--examples/sheet_protection.rb8
-rw-r--r--lib/axlsx/workbook/worksheet/sheet_protection.rb19
2 files changed, 17 insertions, 10 deletions
diff --git a/examples/sheet_protection.rb b/examples/sheet_protection.rb
new file mode 100644
index 00000000..be6cf677
--- /dev/null
+++ b/examples/sheet_protection.rb
@@ -0,0 +1,8 @@
+#!/usr/bin/env ruby -w -s
+# -*- coding: utf-8 -*-
+$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
+require 'axlsx'
+
+p = Axlsx::Package.new
+p.workbook.add_worksheet { |ws| ws.sheet_protection.password = 'fish' }
+p.serialize 'sheet_protection.xlsx'
diff --git a/lib/axlsx/workbook/worksheet/sheet_protection.rb b/lib/axlsx/workbook/worksheet/sheet_protection.rb
index 66687f75..836f6513 100644
--- a/lib/axlsx/workbook/worksheet/sheet_protection.rb
+++ b/lib/axlsx/workbook/worksheet/sheet_protection.rb
@@ -107,7 +107,7 @@ module Axlsx
# If 1 or true then the sheet is protected.
# If 0 or false then the sheet is not protected.
# @return [Boolean]
- # default false
+ # default true
attr_reader :sheet
# If 1 or true then sorting should not be allowed when the sheet is protected.
@@ -141,8 +141,8 @@ module Axlsx
# @option options [String] password. The password required for unlocking. @see SheetProtection#password=
# @option options [Boolean] objects @see SheetProtection#objects
def initialize(options={})
- @sheet = @objects = @scenarios = @select_locked_cells = @select_unlocked_cells = false
- @format_cells = @format_rows = @format_columns = @insert_columns = @insert_rows = @insert_hyperlinks = @delete_columns = @delete_rows = @sort = @auto_filter = @pivot_tables = true
+ @objects = @scenarios = @select_locked_cells = @select_unlocked_cells = false
+ @sheet = @format_cells = @format_rows = @format_columns = @insert_columns = @insert_rows = @insert_hyperlinks = @delete_columns = @delete_rows = @sort = @auto_filter = @pivot_tables = true
options.each do |o|
self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
end
@@ -163,19 +163,18 @@ module Axlsx
@salt_value = @spin_count = @hash_value = v if v == nil
return if v == nil
require 'digest/sha1'
- @spin_count = 10000
- @salt_value = Digest::SHA1.hexdigest(rand(36**8).to_s(36))
-
- salty = @salt_value + v.to_s
- @hash_value = Digest::SHA1.hexdigest(salty)
+ @spin_count = 0
+ @salt_value = Digest::SHA1.new << rand(36**8).to_s(36)
+ @hash_value = @salt_value.to_s + v
+ @hash_value = Digest::SHA1.new << @hash_value
@spin_count.times do |count|
- @hash_value = Digest::SHA1.hexdigest(@hash_value) + count.to_s
+ @hash_value = @hash_value.update(count.to_s.bytes.to_a.pack('L'))
end
end
def to_xml_string(str = '')
str << '<sheetProtection '
- str << instance_values.map{ |k,v| k.gsub(/_(.)/){ $1.upcase } << %{="#{v}"} }.join(' ')
+ str << instance_values.map{ |k,v| k.gsub(/_(.)/){ $1.upcase } << %{="#{v.to_s}"} }.join(' ')
str << '/>'
end
end