Class: Blake2

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

Defined Under Namespace

Classes: Key

Instance Method Summary collapse

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
# File 'ext/blake2_ext/rbext.c', line 59

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

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

  Data_Get_Struct(self, Blake2, blake2);

  blake2s(blake2->output, input, blake2->key_bytes,
      blake2->output_length, input_length, 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]));
    }

    return result;
  } else if(_representation == blake2->to_hex) {
    char * c_str = (char*)ruby_xmalloc(blake2->output_length * sizeof(char) * 2);

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

    result = rb_str_new2(c_str);

    ruby_xfree(c_str);

    return result;
  } else {
    rb_raise(rb_eArgError, "Unknown representation", _representation);
  }
}