class Blake2b

Public Class Methods

bytes(input, key = Blake2b::Key.none, out_len = 32) click to toggle source
# File lib/blake2b.rb, line 10
def self.bytes(input, key = Blake2b::Key.none, out_len = 32)
  check_if_valid!(input, key, out_len)
  Blake2b.new(out_len, key).digest(input, :to_bytes)
end
hex(input, key = Blake2b::Key.none, out_len = 32) click to toggle source
# File lib/blake2b.rb, line 5
def self.hex(input, key = Blake2b::Key.none, out_len = 32)
  check_if_valid!(input, key, out_len)
  Blake2b.new(out_len, key).digest(input, :to_hex)
end
new(p1, p2) click to toggle source
VALUE m_blake2_initialize(VALUE self, VALUE _len, VALUE _key) {
  Blake2 *blake2;
  Data_Get_Struct(self, Blake2, blake2);
  unsigned int i;

  ID bytes_method  = rb_intern("bytes");
  blake2->to_hex   = ID2SYM(rb_intern("to_hex"));
  blake2->to_bytes = ID2SYM(rb_intern("to_bytes"));

  VALUE key_bytes_ary = rb_funcall(_key, bytes_method, 0);
  blake2->key_length  = RARRAY_LEN(key_bytes_ary);
  blake2->key_bytes   = (uint8_t*)malloc(blake2->key_length * sizeof(uint8_t));

  for(i = 0; i < blake2->key_length; i++) {
    VALUE byte           = rb_ary_entry(key_bytes_ary, i);
    blake2->key_bytes[i] = NUM2INT(byte);
  }

  blake2->output_length = NUM2INT(_len);
  blake2->output        = (uint8_t*)malloc(blake2->output_length * sizeof(uint8_t));

  return Qnil;
}

Private Class Methods

check_if_valid!(input, key, out_len) click to toggle source
# File lib/blake2b.rb, line 15
def self.check_if_valid!(input, key, out_len)
  unless input.is_a?(String)
    raise ArgumentError, 'input arg must be a String'
  end

  unless key.is_a?(Blake2b::Key)
    raise ArgumentError, 'key arg must be a Blake2b::Key'
  end

  unless out_len.is_a?(Integer) && out_len.between?(1, 64)
    raise ArgumentError, 'out_len arg must be an Integer between 1 and 64 inclusive'
  end
end

Public Instance Methods

digest(p1, p2) click to toggle source
VALUE m_blake2_digest(VALUE self, VALUE _input, VALUE _representation) {
  Blake2 *blake2;

  char *input           = RSTRING_PTR(_input);
  uint64_t input_length = RSTRING_LEN(_input);
  unsigned int i;

  Data_Get_Struct(self, Blake2, blake2);

  blake2b(blake2->output, blake2->output_length, input, input_length,
      blake2->key_bytes, blake2->key_length);

  VALUE result;

  if(_representation == blake2->to_bytes) {
    result = rb_ary_new2(blake2->output_length);

    for(i = 0; i < blake2->output_length; i++) {
      rb_ary_push(result, INT2NUM(blake2->output[i]));
    }
  } else if(_representation == blake2->to_hex) {
    unsigned long ary_len = blake2->output_length * (unsigned)sizeof(char) * 2;
    char *c_str = (char*)malloc(ary_len + 1);

    for(i = 0; i < blake2->output_length; i++) {
      sprintf(c_str + (i * 2), "%02x", blake2->output[i]);
    }
    c_str[ary_len] = 0;

    result = rb_str_new(c_str, ary_len);

    if((unsigned long)RSTRING_LEN(result) != ary_len) {
      rb_raise(rb_eRuntimeError, "m_blake2_digest: sizes don't match. Ary: %lu != %lu", RSTRING_LEN(result), ary_len);
    }

    free(c_str);
  } else {
    rb_raise(rb_eArgError, "unknown representation :%"PRIsVALUE"", _representation);
  }

  return result;
}