Class: OpenSSL::Digest::Digest

Inherits:
Object
  • Object
show all
Defined in:
ossl_digest.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ossl_digest.c', line 80

static VALUE
ossl_digest_initialize(int argc, VALUE *argv, VALUE self)
{
    EVP_MD_CTX *ctx;
    const EVP_MD *md;
    char *name;
    VALUE type, data;

    rb_scan_args(argc, argv, "11", &type, &data);
    StringValue(type);
    if (!NIL_P(data)) StringValue(data);
    name = StringValuePtr(type);
    
    md = EVP_get_digestbyname(name);
    if (!md) {
	ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name);
    }
    GetDigest(self, ctx);
    EVP_DigestInit_ex(ctx, md, NULL);
    
    if (!NIL_P(data)) return ossl_digest_update(self, data);
    return self;
}

Class Method Details

.digest(str, data) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'ossl_digest.c', line 195

static VALUE
ossl_digest_s_digest(VALUE klass, VALUE str, VALUE data)
{
    VALUE obj = rb_class_new_instance(1, &str, klass);

    ossl_digest_update(obj, data);

    return ossl_digest_digest(obj);
}

.hexdigest(str, data) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'ossl_digest.c', line 205

static VALUE
ossl_digest_s_hexdigest(VALUE klass, VALUE str, VALUE data)
{
    VALUE obj = rb_class_new_instance(1, &str, klass);

    ossl_digest_update(obj, data);

    return ossl_digest_hexdigest(obj);
}

Instance Method Details

#==(other) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ossl_digest.c', line 215

static VALUE
ossl_digest_equal(VALUE self, VALUE other)
{
    EVP_MD_CTX *ctx;
    VALUE str1, str2;

    if (rb_obj_is_kind_of(other, cDigest) == Qtrue) {
	str2 = ossl_digest_digest(other);
    } else {
	StringValue(other);
	str2 = other;
    }
    GetDigest(self, ctx);
    if (RSTRING(str2)->len == EVP_MD_CTX_size(ctx)) {
	str1 = ossl_digest_digest(self);
    } else {
	str1 = ossl_digest_hexdigest(self);
    }
    if (RSTRING(str1)->len == RSTRING(str2)->len
	&& rb_str_cmp(str1, str2) == 0) {
	return Qtrue;
    }

    return Qfalse;
}

#digestObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ossl_digest.c', line 160

static VALUE
ossl_digest_digest(VALUE self)
{
    EVP_MD_CTX *ctx;
    char *buf;
    int buf_len;
    VALUE digest;
	
    GetDigest(self, ctx);
    digest_final(ctx, &buf, &buf_len);
    digest = ossl_buf2str(buf, buf_len);
	
    return digest;
}

#hexdigestObject Also known as: inspect, to_s



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ossl_digest.c', line 175

static VALUE
ossl_digest_hexdigest(VALUE self)
{
    EVP_MD_CTX *ctx;
    char *buf, *hexbuf;
    int buf_len;
    VALUE hexdigest;

    GetDigest(self, ctx);
    digest_final(ctx, &buf, &buf_len);
    if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * buf_len) {
	OPENSSL_free(buf);
	ossl_raise(eDigestError, "Memory alloc error");
    }
    OPENSSL_free(buf);
    hexdigest = ossl_buf2str(hexbuf, 2 * buf_len);

    return hexdigest;
}

#nameObject



241
242
243
244
245
246
247
248
249
# File 'ossl_digest.c', line 241

static VALUE
ossl_digest_name(VALUE self)
{
    EVP_MD_CTX *ctx;

    GetDigest(self, ctx);

    return rb_str_new2(EVP_MD_name(EVP_MD_CTX_md(ctx)));
}

#resetObject



121
122
123
124
125
126
127
128
129
130
# File 'ossl_digest.c', line 121

static VALUE
ossl_digest_reset(VALUE self)
{
    EVP_MD_CTX *ctx;

    GetDigest(self, ctx);
    EVP_DigestInit_ex(ctx, EVP_MD_CTX_md(ctx), NULL);

    return self;
}

#sizeObject



251
252
253
254
255
256
257
258
259
# File 'ossl_digest.c', line 251

static VALUE
ossl_digest_size(VALUE self)
{
    EVP_MD_CTX *ctx;

    GetDigest(self, ctx);

    return INT2NUM(EVP_MD_CTX_size(ctx));
}

#update(data) ⇒ Object Also known as: <<



132
133
134
135
136
137
138
139
140
141
142
# File 'ossl_digest.c', line 132

VALUE
ossl_digest_update(VALUE self, VALUE data)
{
    EVP_MD_CTX *ctx;

    StringValue(data);
    GetDigest(self, ctx);
    EVP_DigestUpdate(ctx, RSTRING(data)->ptr, RSTRING(data)->len);

    return self;
}