Sha256: 5d42ee63a61b1f0e8910a7d64dc93a6085c62523d112e03e230d2401e9400aab

Contents?: true

Size: 820 Bytes

Versions: 20

Compression:

Stored size: 820 Bytes

Contents

class Epics::MGF1
  def initialize(digest = OpenSSL::Digest::SHA256)
    @digest = digest.new
    @hlen = 32
  end

  def generate(seed, masklen)
    if masklen > (2 << 31) * @hlen
      raise ArgumentError, "mask too long"
    end
    t = ""
    divceil(masklen, @hlen).times do |counter|
      t += @digest.digest(seed + i2osp(counter, 4))
    end
    t[0, masklen]
  end

  def i2osp(x, len)
    if x >= 256 ** len
      raise ArgumentError, "integer too large"
    end
    [x].pack("N").gsub(/^\x00+/, '').rjust(len, "\x00")
  end

  def divceil(a, b)
    (a + b - 1) / b
  end

  def xor(a, b)
    if a.size != b.size
      raise ArgumentError, "different length for a and b"
    end
    a = a.unpack('C*')
    b = b.unpack('C*')
    a.size.times do |idx|
      a[idx] ^= b[idx]
    end
    a.pack("C*")
  end

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
epics-1.8.1 lib/epics/mgf.rb
epics-1.8.0 lib/epics/mgf.rb
epics-1.7.2 lib/epics/mgf.rb
epics-1.7.1 lib/epics/mgf.rb
epics-1.7.0 lib/epics/mgf.rb
epics-1.6.0 lib/epics/mgf.rb
epics-1.5.2 lib/epics/mgf.rb
epics-1.5.1 lib/epics/mgf.rb
epics-1.5.0 lib/epics/mgf.rb
epics-1.4.1 lib/epics/mgf.rb
epics-1.4.0 lib/epics/mgf.rb
epics-1.3.1 lib/epics/mgf.rb
epics-1.3.0 lib/epics/mgf.rb
epics-1.2.2 lib/epics/mgf.rb
epics-1.2.1 lib/epics/mgf.rb
epics-1.2.0 lib/epics/mgf.rb
epics-1.1.2 lib/epics/mgf.rb
epics-1.1.1 lib/epics/mgf.rb
epics-1.1.0 lib/epics/mgf.rb
epics-1.0.0 lib/epics/mgf.rb