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