Class: OpenSSL::HMAC

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(key, digest) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'ossl_hmac.c', line 66

static VALUE
ossl_hmac_initialize(VALUE self, VALUE key, VALUE digest)
{
    HMAC_CTX *ctx;

    StringValue(key);
    GetHMAC(self, ctx);
    HMAC_Init(ctx, RSTRING_PTR(key), RSTRING_LENINT(key),
		 GetDigestPtr(digest));

    return self;
}

Class Method Details

.digest(digest, key, data) ⇒ aString

Returns:

  • (aString)


193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'ossl_hmac.c', line 193

static VALUE
ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data)
{
    unsigned char *buf;
    unsigned int buf_len;

    StringValue(key);
    StringValue(data);
    buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LENINT(key),
	       (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len);

    return rb_str_new((const char *)buf, buf_len);
}

.digest(digest, key, data) ⇒ aString

Returns:

  • (aString)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ossl_hmac.c', line 212

static VALUE
ossl_hmac_s_hexdigest(VALUE klass, VALUE digest, VALUE key, VALUE data)
{
    unsigned char *buf;
    char *hexbuf;
    unsigned int buf_len;
    VALUE hexdigest;

    StringValue(key);
    StringValue(data);

    buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LENINT(key),
	       (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len);
    if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * (int)buf_len) {
	ossl_raise(eHMACError, "Cannot convert buf to hexbuf");
    }
    hexdigest = ossl_buf2str(hexbuf, 2 * buf_len);

    return hexdigest;
}

Instance Method Details

#digestaString

Returns:

  • (aString)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ossl_hmac.c', line 131

static VALUE
ossl_hmac_digest(VALUE self)
{
    HMAC_CTX *ctx;
    unsigned char *buf;
    unsigned int buf_len;
    VALUE digest;

    GetHMAC(self, ctx);
    hmac_final(ctx, &buf, &buf_len);
    digest = ossl_buf2str((char *)buf, buf_len);

    return digest;
}

#hexdigestaString Also known as: inspect, to_s

Returns:

  • (aString)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'ossl_hmac.c', line 151

static VALUE
ossl_hmac_hexdigest(VALUE self)
{
    HMAC_CTX *ctx;
    unsigned char *buf;
    char *hexbuf;
    unsigned int buf_len;
    VALUE hexdigest;

    GetHMAC(self, ctx);
    hmac_final(ctx, &buf, &buf_len);
    if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * (int)buf_len) {
	OPENSSL_free(buf);
	ossl_raise(eHMACError, "Memory alloc error");
    }
    OPENSSL_free(buf);
    hexdigest = ossl_buf2str(hexbuf, 2 * buf_len);

    return hexdigest;
}

#resetself

Returns:

  • (self)


177
178
179
180
181
182
183
184
185
186
# File 'ossl_hmac.c', line 177

static VALUE
ossl_hmac_reset(VALUE self)
{
    HMAC_CTX *ctx;

    GetHMAC(self, ctx);
    HMAC_Init(ctx, NULL, 0, NULL);

    return self;
}

#update(string) ⇒ self Also known as: <<

Returns:

  • (self)


99
100
101
102
103
104
105
106
107
108
109
# File 'ossl_hmac.c', line 99

static VALUE
ossl_hmac_update(VALUE self, VALUE data)
{
    HMAC_CTX *ctx;

    StringValue(data);
    GetHMAC(self, ctx);
    HMAC_Update(ctx, (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data));

    return self;
}