Sha256: 082898917b2c3443292c5546005288f8fff3c67f59c4c27a61dbf9f61a1d11f9

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module Gap
    CR_ACQUIRE   = Win32API.new("ADVAPI32", "CryptAcquireContext", "pLLLL", "L")
    CR_GENRANDOM = Win32API.new("ADVAPI32", "CryptGenRandom", "LLp", "L")
    CR_RELEASE   = Win32API.new("ADVAPI32", "CryptReleaseContext", "LL", "L")
    class Random
        REG = {}
        def self.auto_release_proc
            proc{|id|
                if REG.include? id
                    _dispose REG[id]
                    REG.delete id
                end
            }
        end
        
        def self.register_auto_release(id, ptr)
            REG[object_id] = ptr
            ObjectSpace.define_finalizer self, auto_release_proc
        end

        def initialize
            buf = "\0" * 4
            CR_ACQUIRE.call buf, 0, 0, 1, -268435456
            @ptr, = buf.unpack("L")
            self.class.register_auto_release object_id, @ptr
        end

        def bytes(len = 16)
            buf = "\0" * len
            CR_GENRANDOM.call @ptr, len, buf
            buf
        end

        def hex(len = 16)
            bytes(len).unpack("H*").first
        end

        def next_int
            bytes(4).unpack("L").first
        end

        def self._dispose(ptr)
            if ptr
                CR_RELEASE.call ptr, 0
            end
        end

        def dispose
           self.class._dispose @ptr
           @ptr = nil
        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gap50-0.1.1 lib/gap50/samsara/random.rb
gap50-0.1.0 lib/gap50/samsara/random.rb