Class: Blake2b

Inherits:
Object
  • Object
show all
Defined in:
lib/blake2b.rb,
lib/blake2b/key.rb,
ext/blake2b_ext/rbext.c

Defined Under Namespace

Classes: Key

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bytes(input, key = Blake2b::Key.none, out_len = 32) ⇒ Object



10
11
12
13
# 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) ⇒ Object



5
6
7
8
# 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

Instance Method Details

#digest(_input, _representation) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'ext/blake2b_ext/rbext.c', line 59

VALUE m_blake2_digest(VALUE self, VALUE _input, VALUE _representation) {
  Blake2 *blake2;

  char *input           = RSTRING_PTR(_input);
  uint64_t input_length = RSTRING_LEN(_input);
  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; (unsigned)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; (unsigned)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;
}