summaryrefslogtreecommitdiff
path: root/lib/chef/win32/crypto.rb
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-03-24 10:21:14 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2015-03-24 10:21:14 -0700
commitd3e4e565fdf7c210acfd8efd0de6b674883a5e0f (patch)
tree5fd2f1b237e39c81d8a7b54e0cdc0279e7528435 /lib/chef/win32/crypto.rb
parent17014504259f8ccb19d782bd3b63b24657e4c9ca (diff)
parent157852156703daf96366dd773903147baf0292d9 (diff)
downloadchef-7296c5ece82c77334c7fce2787f267b79c4e8736.tar.gz
Merge pull request #3128 from chef/jdm/prepare-12.2.012.2.0.rc.0
prepare 12.2.0 RC 0
Diffstat (limited to 'lib/chef/win32/crypto.rb')
-rw-r--r--lib/chef/win32/crypto.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/chef/win32/crypto.rb b/lib/chef/win32/crypto.rb
new file mode 100644
index 0000000000..79cf51b002
--- /dev/null
+++ b/lib/chef/win32/crypto.rb
@@ -0,0 +1,49 @@
+#
+# Author:: Jay Mundrawala (<jdm@chef.io>)
+# Copyright:: Copyright 2015 Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef/win32/error'
+require 'chef/win32/api/memory'
+require 'chef/win32/api/crypto'
+require 'digest'
+
+class Chef
+ module ReservedNames::Win32
+ class Crypto
+ include Chef::ReservedNames::Win32::API::Crypto
+ extend Chef::ReservedNames::Win32::API::Crypto
+
+ def self.encrypt(str, &block)
+ data_blob = CRYPT_INTEGER_BLOB.new
+ unless CryptProtectData(CRYPT_INTEGER_BLOB.new(str.to_wstring), nil, nil, nil, nil, 0, data_blob)
+ Chef::ReservedNames::Win32::Error.raise!
+ end
+ bytes = data_blob[:pbData].get_bytes(0, data_blob[:cbData])
+ if block
+ block.call(bytes)
+ else
+ Digest.hexencode(bytes)
+ end
+ ensure
+ unless data_blob[:pbData].null?
+ Chef::ReservedNames::Win32::Memory.local_free(data_blob[:pbData])
+ end
+ end
+
+ end
+ end
+end