Module: CcipherFactory::SymKeyCipher::SymKeyEncrypt
- Includes:
- Common, Compression::CompressionHelper, TR::CondUtils
- Included in:
- KCV
- Defined in:
- lib/ccipher_factory/symkey_cipher/symkey_encrypt.rb
Instance Attribute Summary collapse
-
#iv ⇒ Object
Returns the value of attribute iv.
-
#key ⇒ Object
Returns the value of attribute key.
-
#mode ⇒ Object
Returns the value of attribute mode.
Instance Method Summary collapse
Methods included from Compression::CompressionHelper
#compress_data_if_active, #compression_off, #compression_on, #compressor, #decompress_data_if_active, #decompressor, #decompressor_from_encoded, #encode_null_compressor, #is_compression_on?
Methods included from Common
#attach_mode, #cleanup_intOutputBuf, #cleanup_intOutputFile, #detach_mode, #disposeOutput, #intOutputBuf, #intOutputFile, #is_attach_mode?, #is_output_given?, #output, #output_obj, #sanitize_symbol, #write_to_output
Instance Attribute Details
#iv ⇒ Object
Returns the value of attribute iv.
11 12 13 |
# File 'lib/ccipher_factory/symkey_cipher/symkey_encrypt.rb', line 11 def iv @iv end |
#key ⇒ Object
Returns the value of attribute key.
11 12 13 |
# File 'lib/ccipher_factory/symkey_cipher/symkey_encrypt.rb', line 11 def key @key end |
#mode ⇒ Object
Returns the value of attribute mode.
11 12 13 |
# File 'lib/ccipher_factory/symkey_cipher/symkey_encrypt.rb', line 11 def mode @mode end |
Instance Method Details
#encrypt_final ⇒ Object
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 147 148 149 150 151 |
# File 'lib/ccipher_factory/symkey_cipher/symkey_encrypt.rb', line 86 def encrypt_final #if not is_gcm_mode? enc = @cipher.final logger.debug "Cipher final returns #{enc.length} bytes" write_to_output(enc) #end @cipher = nil # this is to clear up the cipher object from memory # including key and IV value # Tested with aes-finder utility on ruby 3.0.2 # https://github.com/mmozeiko/aes-finder GC.start @iv = @cconf.iv if is_empty?(@iv) conv = Ccrypto::UtilFactory.instance(:data_converter) #logger.debug "Key : #{conv.to_hex(@key.key)}" #logger.debug "IV : #{conv.to_hex(@iv)}" #logger.debug "Mode : #{@mode}" #logger.debug "Output : #{conv.to_hex(@output.string)}" #ts = Encoding::ASN1Encoder.instance(:symkey_cipher) ts = BinStruct.instance.struct(:symkey_cipher) if is_empty?(@mode) ts.mode = 0 logger.debug "Encoding null mode" else ts.mode = BTag.constant_value(@mode) logger.debug "Encoding mode #{@mode}" end if is_empty?(@iv) ts.iv = "" logger.debug "Encoding empty IV" else ts.iv = @iv logger.debug "Encoding IV of #{@iv.length} bytes" end if is_compression_on? ts.compression = compressor.compress_final logger.tdebug :symkey_enc, "Plain : #{@totalPlain} / Compressed : #{@totalCompressed} = #{(@totalCompressed*1.0)/@totalPlain*100} %" else ts.compression = BinStruct.instance.struct(:compression_none).encoded end if @cconf.respond_to?(:auth_tag) if is_empty?(@cconf.auth_tag) ts.auth_tag = "" logger.debug "Encoding empty AuthTag" else ts.auth_tag = @cconf.auth_tag logger.debug "Encoding AuthTag of #{@cconf.auth_tag.length}" end else ts.auth_tag = "" logger.debug "AuthTag not relevent" end #logger.debug "encoding : #{ts.inspect}" ts.encoded end |
#encrypt_init(*args, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ccipher_factory/symkey_cipher/symkey_encrypt.rb', line 13 def encrypt_init(*args, &block) raise SymKeyCipherError, "Encryption key is required" if is_empty?(@key) raise SymKeyCipherError, "SymKey object is required" if not @key.is_a?(SymKey) raise SymKeyCipherError, "Cipher requires output to be set" if not is_output_given? #_, _, mode = SymKeyCipher.algo_default(@key.keytype) @cconf = SymKeyCipher.algo_default(@key.keytype) @cconf.key = @key.key @cconf.keysize = @key.keysize @cconf.iv = @iv if not_empty?(@iv) if is_empty?(@mode) @mode = @cconf.mode else @cconf.mode = @mode end #spec = SymKeyCipher.key_to_spec(@key, @mode) logger.tdebug :symkey_enc, "Encrypt cipher spec : #{@cconf}" @cconf.cipherOps = :encrypt begin @cipher = Ccrypto::AlgoFactory.engine(@cconf) #rescue Ccrypto::CipherEngineException => ex rescue Exception => ex raise SymKeyCipherError, ex end #@cipher = OpenSSL::Cipher.new(cconf.provider_config) #@cipher.encrypt #@cipher.key = @key.key #if is_empty?(@iv) # @iv = @cipher.random_iv #else # @cipher.iv = @iv #end if is_compression_on? logger.tdebug :symkey_enc, "Compression on" else logger.tdebug :symkey_enc, "Compression off" end @totalPlain = 0 @totalCompressed = 0 if block instance_eval(&block) encrypt_final else self end end |
#encrypt_update(val) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ccipher_factory/symkey_cipher/symkey_encrypt.rb', line 71 def encrypt_update(val) if not_empty?(val) @totalPlain += val.length cval = compress_data_if_active(val) @totalCompressed += cval.length enc = @cipher.update(cval) if not_empty?(enc) write_to_output(enc) end end end |