Class: Digest::Blake2b

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

Defined Under Namespace

Classes: Key

Constant Summary collapse

VERSION =
'0.0.5'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



14
15
16
17
# File 'lib/digest/blake2b.rb', line 14

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



9
10
11
12
# File 'lib/digest/blake2b.rb', line 9

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



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
101
102
# File 'ext/digest/blake2b/ext/rbext.c', line 61

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;
}