Sha256: f4c7f47a5a69eb443b39136975b08c70f5ad7666a2f26b4bd0e3e9b50b82b4d1

Contents?: true

Size: 1.85 KB

Versions: 6

Compression:

Stored size: 1.85 KB

Contents

module Browser

# Implements (parts of) the web crypto interface
#
# https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API
class Crypto
  include NativeCachedWrapper

  class Digest
    def initialize(buf)
      @buffer = Buffer.new(buf)
    end

    attr_reader :buffer

    # Convert a digest to a hexadecimal string
    def to_hex
      buffer.to_a.map { |i| "%02x" % i }.join
    end

    # Convert a digest to a binary string
    def to_s
      buffer.to_a.map { |i| "%c" % i }.join
    end

    # Convert a digest to a Base64-encoded string
    #
    # You will need to `require "base64"`
    def to_b64
      Base64.strict_encode64(to_s)
    end

    # Convert a digest to a urlsafe Base64-encoded string
    #
    # You will need to `require "base64"`
    def to_u64(padding: false)
      Base64.urlsafe_encode64(to_s, padding: padding)
    end
  end

  # Compute a cryptographic digest of data (a Buffer). If block is given,
  # it will call a block, otherwise it will return a Promise that will
  # return once a digest is computed.
  #
  # Allowed values for algo: SHA-1, SHA-256 (default), SHA-368, SHA-512.
  #
  # The block/promise will be given an argument of type {Digest} which can
  # be used to format a digest.
  #
  # Example:
  # ```
  # Browser::Blob.new(['test']).buffer.then { |b|
  #   $window.crypto.digest(b)
  # }.then { |d|
  #   puts d.to_hex
  # }
  # ```
  def digest data, algo = 'SHA-256', &block
    promise = nil
    unless block_given?
      promise = Promise.new
      block = proc { |i| promise.resolve(i) }
    end
    resblock = proc { |i| block.call(Digest.new(i)) }

    `#@native.subtle.digest(algo, #{Native.convert(data)}).then(#{resblock.to_n})`
    promise
  end
end

class Window
  # @!attribute [r] crypto
  # @return [Crypto] the crypto interface of this window
  def crypto
    @crypto ||= Crypto.new(`#@native.crypto`)
  end
end

end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
opal-browser-0.3.4 opal/browser/crypto.rb
atome-opal-browser-0.3.9.5 opal/browser/crypto.rb
opal-browser-0.3.3 opal/browser/crypto.rb
opal-browser-0.3.2 opal/browser/crypto.rb
opal-browser-0.3.1 opal/browser/crypto.rb
opal-browser-0.3.0 opal/browser/crypto.rb