Debian Codegraph @

plugin/world/blowfish_ecb/lib/ecb.rb

raw symbols SCIP
1 module Blowfish::ECB
2 class << self
3 # call-seq:
4 # Blowfish::ECB.encrypt(string, Key) -> string
5 #
6 # Encrypts the data (as in byte sequence) with given key.
7 # Since the data is a byte sequence, its encoding must be ASCII-8BIT.
8 def encrypt(data, key)
9 if data.size % 8 != 0
10 raise "data size must be multiple of 8, but got: #{data.size}"
11 end
12
13 res = ''
14 0.step(data.size - 1, 8).each do |i|
15 l, r = data.byteslice(i, 8).unpack('I>I>')
16 res += Common.encrypt(l, r, key).pack('I>I>')
17 end
18 res
19 end
20
21 # call-seq:
22 # Blowfish::ECB.decrypt(string, Key) -> string
23 #
24 # Decrypts the data (as in byte sequence) with given key.
25 # Since the data is a byte sequence, its encoding must be ASCII-8BIT.
26 def decrypt(data, key)
27 if data.encoding != Encoding::ASCII_8BIT
28 raise "data.encoding must be ASCII-8BIT, but got: #{data.encoding}"
29 end
30 if data.size % 8 != 0
31 raise "data size must be multiple of 8, but got: #{data.size}"
32 end
33
34 res = ''
35 0.step(data.size - 1, 8).each do |i|
36 l, r = data.byteslice(i, 8).unpack('I>I>')
37 res += Common.decrypt(l, r, key).pack('I>I>')
38 end
39 res
40 end
41 end
42 end