Class: Windows::CNG
- Inherits:
-
Object
- Object
- Windows::CNG
- Includes:
- CNGConstants, CNGFunctions, CNGHelper, CNGStructs, MiscFunctions
- Defined in:
- lib/windows/cng.rb
Constant Summary collapse
- VERSION =
The version of the windows-cng library.
'0.0.1'
Constants included from CNGConstants
Windows::CNGConstants::BCRYPT_3DES_112_ALGORITHM, Windows::CNGConstants::BCRYPT_3DES_ALGORITHM, Windows::CNGConstants::BCRYPT_AES_ALGORITHM, Windows::CNGConstants::BCRYPT_AES_CMAC_ALGORITHM, Windows::CNGConstants::BCRYPT_AES_GMAC_ALGORITHM, Windows::CNGConstants::BCRYPT_ALGORITHM_NAME, Windows::CNGConstants::BCRYPT_AUTH_TAG_LENGTH, Windows::CNGConstants::BCRYPT_BLOCK_LENGTH, Windows::CNGConstants::BCRYPT_BLOCK_SIZE_LIST, Windows::CNGConstants::BCRYPT_CAPI_KDF_ALGORITHM, Windows::CNGConstants::BCRYPT_CHAINING_MODE, Windows::CNGConstants::BCRYPT_DESX_ALGORITHM, Windows::CNGConstants::BCRYPT_DES_ALGORITHM, Windows::CNGConstants::BCRYPT_DH_ALGORITHM, Windows::CNGConstants::BCRYPT_DSA_ALGORITHM, Windows::CNGConstants::BCRYPT_ECDH_P256_ALGORITHM, Windows::CNGConstants::BCRYPT_ECDH_P384_ALGORITHM, Windows::CNGConstants::BCRYPT_ECDH_P521_ALGORITHM, Windows::CNGConstants::BCRYPT_ECDSA_P256_ALGORITHM, Windows::CNGConstants::BCRYPT_ECDSA_P384_ALGORITHM, Windows::CNGConstants::BCRYPT_ECDSA_P521_ALGORITHM, Windows::CNGConstants::BCRYPT_HASH_BLOCK_LENGTH, Windows::CNGConstants::BCRYPT_HASH_LENGTH, Windows::CNGConstants::BCRYPT_MD2_ALGORITHM, Windows::CNGConstants::BCRYPT_MD4_ALGORITHM, Windows::CNGConstants::BCRYPT_MD5_ALGORITHM, Windows::CNGConstants::BCRYPT_OBJECT_LENGTH, Windows::CNGConstants::BCRYPT_PBKDF2_ALGORITHM, Windows::CNGConstants::BCRYPT_RC2_ALGORITHM, Windows::CNGConstants::BCRYPT_RC4_ALGORITHM, Windows::CNGConstants::BCRYPT_RNG_ALGORITHM, Windows::CNGConstants::BCRYPT_RNG_DUAL_EC_ALGORITHM, Windows::CNGConstants::BCRYPT_RNG_FIPS186_DSA_ALGORITHM, Windows::CNGConstants::BCRYPT_RSA_ALGORITHM, Windows::CNGConstants::BCRYPT_RSA_SIGN_ALGORITHM, Windows::CNGConstants::BCRYPT_SHA1_ALGORITHM, Windows::CNGConstants::BCRYPT_SHA256_ALGORITHM, Windows::CNGConstants::BCRYPT_SHA384_ALGORITHM, Windows::CNGConstants::BCRYPT_SHA512_ALGORITHM, Windows::CNGConstants::BCRYPT_SP800108_CTR_HMAC_ALGORITHM, Windows::CNGConstants::BCRYPT_SP80056A_CONCAT_ALGORITHM, Windows::CNGConstants::STATUS_SEVERITY_ERROR, Windows::CNGConstants::STATUS_SEVERITY_INFORMATIONAL, Windows::CNGConstants::STATUS_SEVERITY_SUCCESS, Windows::CNGConstants::STATUS_SEVERITY_WARNING, Windows::CNGConstants::STATUS_SUCCESS
Instance Method Summary collapse
-
#close ⇒ Object
Closes the windows-cng object.
-
#hash(data) ⇒ Object
Returns a hash of
data
using the algorithm used in the constructor. -
#initialize(algorithm = BCRYPT_SHA256_ALGORITHM, implementation = nil, flags = 0) ⇒ CNG
constructor
Creates and returns a new Windows::CNG object.
Constructor Details
#initialize(algorithm = BCRYPT_SHA256_ALGORITHM, implementation = nil, flags = 0) ⇒ CNG
Creates and returns a new Windows::CNG object.
The algorithm
argument specifies the type of algorithm to use for the various crypto methods. The default is SHA256.
The implementation
identifies the specific provider to load. This is the registered alias of the cryptographic primitive provider. By default this is nil.
The flags argument can be one or more of the following values:
-
BCRYPT_ALG_HANDLE_HMAC_FLAG
-
BCRYPT_PROV_DISPATCH
-
BCRYPT_HASH_REUSABLE_FLAG
See the MSDN documentation for details of what each flag does.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/windows/cng.rb', line 34 def initialize(algorithm = BCRYPT_SHA256_ALGORITHM, implementation = nil, flags = 0) @algorithm = algorithm.wincode @implementation = implementation ? implementation.wincode : implementation @flags = flags ptr = FFI::MemoryPointer.new(:pointer) status = BCryptOpenAlgorithmProvider( ptr, @algorithm, @implementation, @flags ) if status != 0 raise SystemCallError.new('BCryptOpenAlgorithmProvider', status) end @handle = ptr.read_pointer ObjectSpace.define_finalizer(self, self.class.finalize(@handle)) end |
Instance Method Details
#close ⇒ Object
Closes the windows-cng object. This is not explicitly required, since it will automatically be called once your object goes out of scope, but it is good form.
152 153 154 155 156 157 158 |
# File 'lib/windows/cng.rb', line 152 def close status = BCryptCloseAlgorithmProvider(@handle, 0) if status != 0 raise SystemCallError.new('BCryptCloseAlgorithmProvider', status) end end |
#hash(data) ⇒ Object
Returns a hash of data
using the algorithm used in the constructor.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/windows/cng.rb', line 59 def hash(data) cbhash_object = FFI::MemoryPointer.new(:ulong) cbdata = FFI::MemoryPointer.new(:ulong) status = BCryptGetProperty( @handle, BCRYPT_OBJECT_LENGTH.wincode, cbhash_object, cbhash_object.size, cbdata, 0 ) if status != 0 raise SystemCallError.new('BCryptGetProperty', status) end begin pbhash_object = HeapAlloc(GetProcessHeap(), 0, cbhash_object.read_ulong) if pbhash_object.null? raise SystemCallError.new('HeapAlloc', FFI.errno) end cbhash = FFI::MemoryPointer.new(:ulong) cbdata.clear status = BCryptGetProperty( @handle, BCRYPT_HASH_LENGTH.wincode, cbhash, cbhash.size, cbdata, 0 ) if status != 0 raise SystemCallError.new('BCryptGetProperty', status) end cbhash = cbhash.read_ulong pbhash = HeapAlloc(GetProcessHeap(), 0, cbhash) if pbhash.null? raise SystemCallError.new('HeapAlloc', FFI.errno) end ptr = FFI::MemoryPointer.new(:pointer) status = BCryptCreateHash( @handle, ptr, pbhash_object, cbhash_object.read_ulong, nil, 0, 0 ) if status != 0 raise SystemCallError.new('BCryptCreateHash', status) end hhash = ptr.read_pointer status = BCryptHashData(hhash, data, data.size, 0) if status != 0 raise SystemCallError.new('BCryptHashData', status) end status = BCryptFinishHash(hhash, pbhash, cbhash, 0) if status != 0 raise SystemCallError.new('BCryptFinishHash', status) end pbhash.read_bytes(cbhash) ensure if pbhash_object && !pbhash_object.null? HeapFree(GetProcessHeap(), 0, pbhash_object) end if pbhash && !pbhash.null? HeapFree(GetProcessHeap(), 0, pbhash) end end end |