krypt-core 0.0.1 → 0.0.2.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +14 -0
  3. data/Rakefile +88 -0
  4. data/ext/krypt/core/krypt-core.c +1 -2
  5. data/ext/krypt/core/krypt-core.h +0 -12
  6. data/ext/krypt/core/krypt_asn1_parser.c +2 -2
  7. data/ext/krypt/core/krypt_missing.c +75 -0
  8. data/ext/krypt/core/krypt_missing.h +5 -0
  9. data/ext/krypt/core/krypt_pem.c +1 -1
  10. data/lib/{krypt-core.rb → krypt/core.rb} +8 -1
  11. metadata +40 -80
  12. data/ext/krypt/core/Makefile +0 -221
  13. data/ext/krypt/core/extconf.h +0 -8
  14. data/ext/krypt/core/krypt-core.o +0 -0
  15. data/ext/krypt/core/krypt-provider.h +0 -86
  16. data/ext/krypt/core/krypt_asn1-internal.o +0 -0
  17. data/ext/krypt/core/krypt_asn1.o +0 -0
  18. data/ext/krypt/core/krypt_asn1_codec.o +0 -0
  19. data/ext/krypt/core/krypt_asn1_in_adapter.o +0 -0
  20. data/ext/krypt/core/krypt_asn1_in_chunked.o +0 -0
  21. data/ext/krypt/core/krypt_asn1_in_definite.o +0 -0
  22. data/ext/krypt/core/krypt_asn1_parser.o +0 -0
  23. data/ext/krypt/core/krypt_asn1_template.o +0 -0
  24. data/ext/krypt/core/krypt_asn1_template_encoder.o +0 -0
  25. data/ext/krypt/core/krypt_asn1_template_parser.o +0 -0
  26. data/ext/krypt/core/krypt_b64.o +0 -0
  27. data/ext/krypt/core/krypt_digest.c +0 -391
  28. data/ext/krypt/core/krypt_digest.h +0 -51
  29. data/ext/krypt/core/krypt_digest.o +0 -0
  30. data/ext/krypt/core/krypt_error.o +0 -0
  31. data/ext/krypt/core/krypt_hex.o +0 -0
  32. data/ext/krypt/core/krypt_io.o +0 -0
  33. data/ext/krypt/core/krypt_io_in_pem.o +0 -0
  34. data/ext/krypt/core/krypt_missing.o +0 -0
  35. data/ext/krypt/core/krypt_pem.o +0 -0
  36. data/ext/krypt/core/krypt_provider-internal.h +0 -40
  37. data/ext/krypt/core/krypt_provider.c +0 -136
  38. data/ext/krypt/core/krypt_provider.o +0 -0
  39. data/ext/krypt/core/kryptcore.so +0 -0
  40. data/ext/krypt/core/mkmf.log +0 -130
  41. data/lib/krypt-core/version.rb +0 -3
  42. data/lib/kryptcore.so +0 -0
  43. data/spec/README +0 -2
  44. data/test/README +0 -2
  45. data/test/res/certificate.cer +0 -0
  46. data/test/resources.rb +0 -48
  47. data/test/scratch.rb +0 -17
@@ -1,221 +0,0 @@
1
-
2
- SHELL = /bin/sh
3
-
4
- # V=0 quiet, V=1 verbose. other values don't work.
5
- V = 0
6
- Q1 = $(V:1=)
7
- Q = $(Q1:0=@)
8
- ECHO1 = $(V:1=@:)
9
- ECHO = $(ECHO1:0=@echo)
10
-
11
- #### Start of system configuration section. ####
12
-
13
- srcdir = .
14
- topdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
15
- hdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
16
- arch_hdrdir = $(hdrdir)/$(arch)
17
- VPATH = $(srcdir):$(topdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
18
- prefix = $(DESTDIR)/home/martin/ruby
19
- rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
20
- exec_prefix = $(prefix)
21
- vendorhdrdir = $(rubyhdrdir)/vendor_ruby
22
- sitehdrdir = $(rubyhdrdir)/site_ruby
23
- rubyhdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
24
- vendordir = $(rubylibprefix)/vendor_ruby
25
- sitedir = $(rubylibprefix)/site_ruby
26
- ridir = $(datarootdir)/$(RI_BASE_NAME)
27
- mandir = $(datarootdir)/man
28
- localedir = $(datarootdir)/locale
29
- libdir = $(exec_prefix)/lib
30
- psdir = $(docdir)
31
- pdfdir = $(docdir)
32
- dvidir = $(docdir)
33
- htmldir = $(docdir)
34
- infodir = $(datarootdir)/info
35
- docdir = $(datarootdir)/doc/$(PACKAGE)
36
- oldincludedir = $(DESTDIR)/usr/include
37
- includedir = $(prefix)/include
38
- localstatedir = $(prefix)/var
39
- sharedstatedir = $(prefix)/com
40
- sysconfdir = $(prefix)/etc
41
- datadir = $(datarootdir)
42
- datarootdir = $(prefix)/share
43
- libexecdir = $(exec_prefix)/libexec
44
- sbindir = $(exec_prefix)/sbin
45
- bindir = $(exec_prefix)/bin
46
- rubylibdir = $(rubylibprefix)/$(ruby_version)
47
- archdir = $(rubylibdir)/$(arch)
48
- sitelibdir = $(sitedir)/$(ruby_version)
49
- sitearchdir = $(sitelibdir)/$(sitearch)
50
- vendorlibdir = $(vendordir)/$(ruby_version)
51
- vendorarchdir = $(vendorlibdir)/$(sitearch)
52
-
53
-
54
- CC = gcc
55
- CXX = g++
56
- LIBRUBY = $(LIBRUBY_A)
57
- LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
58
- LIBRUBYARG_SHARED = -Wl,-R -Wl,$(libdir) -L$(libdir)
59
- LIBRUBYARG_STATIC = -Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static
60
- empty =
61
- OUTFLAG = -o $(empty)
62
- COUTFLAG = -o $(empty)
63
-
64
- RUBY_EXTCONF_H = extconf.h
65
- cflags = $(optflags) $(debugflags) $(warnflags)
66
- optflags = -O3 -fno-fast-math
67
- debugflags = -ggdb3
68
- warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration
69
- CFLAGS = -fPIC $(cflags) $(ARCH_FLAG)
70
- INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
71
- DEFS =
72
- CPPFLAGS = -DRUBY_EXTCONF_H=\"$(RUBY_EXTCONF_H)\" $(DEFS) $(cppflags)
73
- CXXFLAGS = $(CFLAGS) $(cxxflags)
74
- ldflags = -L. -fstack-protector -rdynamic -Wl,-export-dynamic
75
- dldflags =
76
- ARCH_FLAG =
77
- DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
78
- LDSHARED = $(CC) -shared
79
- LDSHAREDXX = $(CXX) -shared
80
- AR = ar
81
- EXEEXT =
82
-
83
- RUBY_BASE_NAME = ruby
84
- RUBY_INSTALL_NAME = ruby
85
- RUBY_SO_NAME = ruby
86
- arch = x86_64-linux
87
- sitearch = $(arch)
88
- ruby_version = 2.0.0
89
- ruby = $(bindir)/ruby
90
- RUBY = $(ruby)
91
- ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h $(RUBY_EXTCONF_H)
92
-
93
- RM = rm -f
94
- RM_RF = $(RUBY) -run -e rm -- -rf
95
- RMDIRS = rmdir --ignore-fail-on-non-empty -p
96
- MAKEDIRS = /bin/mkdir -p
97
- INSTALL = /usr/bin/install -c
98
- INSTALL_PROG = $(INSTALL) -m 0755
99
- INSTALL_DATA = $(INSTALL) -m 644
100
- COPY = cp
101
- TOUCH = exit >
102
-
103
- #### End of system configuration section. ####
104
-
105
- preload =
106
-
107
- libpath = . $(libdir)
108
- LIBPATH = -L. -L$(libdir) -Wl,-R$(libdir)
109
- DEFFILE =
110
-
111
- CLEANFILES = mkmf.log
112
- DISTCLEANFILES =
113
- DISTCLEANDIRS =
114
-
115
- extout =
116
- extout_prefix =
117
- target_prefix =
118
- LOCAL_LIBS =
119
- LIBS = -lpthread -lrt -ldl -lcrypt -lm -lc
120
- ORIG_SRCS = krypt_asn1_in_adapter.c krypt_asn1_in_definite.c krypt_io_in_pem.c krypt_hex.c krypt_provider.c krypt_asn1-internal.c krypt_error.c krypt_asn1_template_encoder.c krypt_asn1_parser.c krypt-core.c krypt_io.c krypt_digest.c krypt_missing.c krypt_asn1_template_parser.c krypt_asn1_template.c krypt_pem.c krypt_asn1_codec.c krypt_b64.c krypt_asn1_in_chunked.c krypt_asn1.c
121
- SRCS = $(ORIG_SRCS)
122
- OBJS = krypt_asn1_in_adapter.o krypt_asn1_in_definite.o krypt_io_in_pem.o krypt_hex.o krypt_provider.o krypt_asn1-internal.o krypt_error.o krypt_asn1_template_encoder.o krypt_asn1_parser.o krypt-core.o krypt_io.o krypt_digest.o krypt_missing.o krypt_asn1_template_parser.o krypt_asn1_template.o krypt_pem.o krypt_asn1_codec.o krypt_b64.o krypt_asn1_in_chunked.o krypt_asn1.o
123
- HDRS = $(srcdir)/krypt_io.h $(srcdir)/krypt_missing.h $(srcdir)/krypt-core.h $(srcdir)/binyo-io-buffer.h $(srcdir)/krypt_asn1.h $(srcdir)/krypt_asn1_template-internal.h $(srcdir)/binyo-error.h $(srcdir)/krypt_hex.h $(srcdir)/krypt_digest.h $(srcdir)/binyo-io.h $(srcdir)/krypt_b64.h $(srcdir)/krypt_asn1-internal.h $(srcdir)/krypt_b64-internal.h $(srcdir)/extconf.h $(srcdir)/krypt_error.h $(srcdir)/krypt_hex-internal.h $(srcdir)/krypt-provider.h $(srcdir)/krypt_provider-internal.h $(srcdir)/krypt_asn1_template.h
124
- TARGET = kryptcore
125
- TARGET_NAME = kryptcore
126
- TARGET_ENTRY = Init_$(TARGET_NAME)
127
- DLLIB = $(TARGET).so
128
- EXTSTATIC =
129
- STATIC_LIB =
130
-
131
- BINDIR = $(bindir)
132
- RUBYCOMMONDIR = $(sitedir)$(target_prefix)
133
- RUBYLIBDIR = /home/martin/Projekte/krypt/emboss/krypt-core-c/lib$(target_prefix)
134
- RUBYARCHDIR = /home/martin/Projekte/krypt/emboss/krypt-core-c/lib$(target_prefix)
135
- HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
136
- ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
137
-
138
- TARGET_SO = $(DLLIB)
139
- CLEANLIBS = $(TARGET).so
140
- CLEANOBJS = *.o *.bak
141
-
142
- all: $(DLLIB)
143
- static: $(STATIC_LIB)
144
- .PHONY: all install static install-so install-rb
145
- .PHONY: clean clean-so clean-static clean-rb
146
-
147
- clean-static::
148
- clean-rb-default::
149
- clean-rb::
150
- clean-so::
151
- clean: clean-so clean-static clean-rb-default clean-rb
152
- -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
153
-
154
- distclean-rb-default::
155
- distclean-rb::
156
- distclean-so::
157
- distclean-static::
158
- distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
159
- -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
160
- -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
161
- -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
162
-
163
- realclean: distclean
164
- install: install-so install-rb
165
-
166
- install-so: $(DLLIB)
167
- $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
168
- clean-static::
169
- -$(Q)$(RM) $(STATIC_LIB)
170
- install-rb: pre-install-rb install-rb-default
171
- install-rb-default: pre-install-rb-default
172
- pre-install-rb: Makefile
173
- pre-install-rb-default: Makefile
174
- pre-install-rb-default:
175
- $(ECHO) installing default kryptcore libraries
176
- ./.RUBYARCHDIR.time:
177
- $(Q) $(MAKEDIRS) $(RUBYARCHDIR)
178
- $(Q) $(TOUCH) $@
179
-
180
- site-install: site-install-so site-install-rb
181
- site-install-so: install-so
182
- site-install-rb: install-rb
183
-
184
- .SUFFIXES: .c .m .cc .mm .cxx .cpp .C .o
185
-
186
- .cc.o:
187
- $(ECHO) compiling $(<)
188
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
189
-
190
- .mm.o:
191
- $(ECHO) compiling $(<)
192
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
193
-
194
- .cxx.o:
195
- $(ECHO) compiling $(<)
196
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
197
-
198
- .cpp.o:
199
- $(ECHO) compiling $(<)
200
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
201
-
202
- .C.o:
203
- $(ECHO) compiling $(<)
204
- $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
205
-
206
- .c.o:
207
- $(ECHO) compiling $(<)
208
- $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
209
-
210
- .m.o:
211
- $(ECHO) compiling $(<)
212
- $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
213
-
214
- $(DLLIB): $(OBJS) Makefile
215
- $(ECHO) linking shared-object $(DLLIB)
216
- -$(Q)$(RM) $(@)
217
- $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
218
-
219
-
220
-
221
- $(OBJS): $(HDRS) $(ruby_headers)
@@ -1,8 +0,0 @@
1
- #ifndef EXTCONF_H
2
- #define EXTCONF_H
3
- #define HAVE_RUBY_IO_H 1
4
- #define HAVE_RB_BIG_PACK 1
5
- #define HAVE_RB_ENUMERATORIZE 1
6
- #define HAVE_RB_STR_ENCODE 1
7
- #define HAVE_GMTIME_R 1
8
- #endif
Binary file
@@ -1,86 +0,0 @@
1
- /*
2
- * krypt-core API - C implementation
3
- *
4
- * Copyright (c) 2011-2013
5
- * Hiroshi Nakamura <nahi@ruby-lang.org>
6
- * Martin Bosslet <martin.bosslet@gmail.com>
7
- * All rights reserved.
8
- *
9
- * Permission is hereby granted, free of charge, to any person obtaining
10
- * a copy of this software and associated documentation files (the
11
- * "Software"), to deal in the Software without restriction, including
12
- * without limitation the rights to use, copy, modify, merge, publish,
13
- * distribute, sublicense, and/or sell copies of the Software, and to
14
- * permit persons to whom the Software is furnished to do so, subject to
15
- * the following conditions:
16
- *
17
- * The above copyright notice and this permission notice shall be
18
- * included in all copies or substantial portions of the Software.
19
- *
20
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
- */
28
-
29
- #ifndef _KRYPT_PROVIDER_H_
30
- #define _KRYPT_PROVIDER_H_
31
-
32
-
33
- #ifndef _RSTRING_NOT_MODIFIED
34
- #define RSTRING_NOT_MODIFIED 1
35
- #endif
36
-
37
- #ifndef RUBY_READONLY_STRING
38
- #define RUBY_READONLY_STRING 1
39
- #endif
40
-
41
- #include <ruby.h>
42
-
43
- #ifndef KRYPT_OK
44
- #define KRYPT_OK 1
45
- #endif
46
- #ifndef KRYPT_ERR
47
- #define KRYPT_ERR -1
48
- #endif
49
-
50
- typedef struct krypt_provider_st krypt_provider;
51
-
52
- /* Message digest */
53
- typedef struct krypt_interface_md_st krypt_interface_md;
54
-
55
- typedef struct krypt_md_st {
56
- krypt_provider *provider;
57
- krypt_interface_md *methods;
58
- } krypt_md;
59
-
60
- struct krypt_interface_md_st {
61
- int (*md_reset)(krypt_md *md);
62
- int (*md_update)(krypt_md *md, const void *data, size_t len);
63
- int (*md_final)(krypt_md *md, uint8_t ** digest, size_t *len);
64
- int (*md_digest)(krypt_md *md, const uint8_t *data, size_t len, uint8_t **digest, size_t *digest_len);
65
- int (*md_digest_length)(krypt_md *md, size_t *len);
66
- int (*md_block_length)(krypt_md *md, size_t *block_len);
67
- int (*md_name)(krypt_md *md, const char **name);
68
- void (*mark)(krypt_md *md);
69
- void (*free)(krypt_md *md);
70
- };
71
-
72
- /* Provider */
73
- struct krypt_provider_st {
74
- const char *name;
75
- krypt_md *(*md_new_oid)(krypt_provider *provider, const char *oid);
76
- krypt_md *(*md_new_name)(krypt_provider *provider, const char *name);
77
- };
78
-
79
- /* Can be called from within a provider implementation to indicate errors */
80
- void krypt_error_add(const char * format, ...);
81
-
82
- /* May be used to register a singleton provider upon initialization */
83
- void krypt_provider_register(krypt_provider *provider);
84
-
85
- #endif /* _KRYPT_PROVIDER_H_ */
86
-
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1,391 +0,0 @@
1
- /*
2
- * krypt-core API - C implementation
3
- *
4
- * Copyright (c) 2011-2013
5
- * Hiroshi Nakamura <nahi@ruby-lang.org>
6
- * Martin Bosslet <martin.bosslet@gmail.com>
7
- * All rights reserved.
8
- *
9
- * Permission is hereby granted, free of charge, to any person obtaining
10
- * a copy of this software and associated documentation files (the
11
- * "Software"), to deal in the Software without restriction, including
12
- * without limitation the rights to use, copy, modify, merge, publish,
13
- * distribute, sublicense, and/or sell copies of the Software, and to
14
- * permit persons to whom the Software is furnished to do so, subject to
15
- * the following conditions:
16
- *
17
- * The above copyright notice and this permission notice shall be
18
- * included in all copies or substantial portions of the Software.
19
- *
20
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
- */
28
-
29
- #include "krypt-core.h"
30
-
31
- VALUE mKryptDigest;
32
- VALUE eKryptDigestError;
33
-
34
- VALUE cKryptNativeDigest;
35
-
36
- /** public internal digest API **/
37
-
38
- #define int_check_provider_has(p, m) if (!(p) || !(p)->m) return NULL;
39
- #define int_check_digest_has(d, m) if (!(d) || !(d)->methods || !(d)->methods->m) return KRYPT_ERR;
40
-
41
- krypt_md *
42
- krypt_md_oid_new(krypt_provider *provider, const char *oid)
43
- {
44
- int_check_provider_has(provider, md_new_oid);
45
- return provider->md_new_oid(provider, oid);
46
- }
47
-
48
- krypt_md *
49
- krypt_md_name_new(krypt_provider *provider, const char *name)
50
- {
51
- int_check_provider_has(provider, md_new_name);
52
- return provider->md_new_name(provider, name);
53
- }
54
-
55
- krypt_md *
56
- krypt_md_new(krypt_provider *provider, const char *name_or_oid)
57
- {
58
- krypt_md *ret = NULL;
59
- if ((ret = krypt_md_name_new(provider, name_or_oid)))
60
- return ret;
61
- return krypt_md_oid_new(provider, name_or_oid);
62
- }
63
-
64
- int
65
- krypt_md_reset(krypt_md *md)
66
- {
67
- int_check_digest_has(md, md_reset);
68
- return md->methods->md_reset(md);
69
- }
70
-
71
- int
72
- krypt_md_update(krypt_md *md, const void *data, size_t len)
73
- {
74
- int_check_digest_has(md, md_update);
75
- return md->methods->md_update(md, data, len);
76
- }
77
-
78
- int
79
- krypt_md_final(krypt_md *md, uint8_t **digest, size_t *len)
80
- {
81
- int_check_digest_has(md, md_final);
82
- if (md->methods->md_final(md, digest, len) == KRYPT_ERR) return KRYPT_ERR;
83
- return krypt_md_reset(md);
84
- }
85
-
86
- int
87
- krypt_md_digest(krypt_md *md, const uint8_t *data, size_t len, uint8_t **digest, size_t *digest_len)
88
- {
89
- int_check_digest_has(md, md_digest);
90
- if(md->methods->md_digest(md, data, len, digest, digest_len) == KRYPT_ERR) return KRYPT_ERR;
91
- return krypt_md_reset(md);
92
- }
93
-
94
- int
95
- krypt_md_digest_length(krypt_md *md, size_t *len)
96
- {
97
- int_check_digest_has(md, md_digest_length);
98
- return md->methods->md_digest_length(md, len);
99
- }
100
-
101
- int
102
- krypt_md_block_length(krypt_md *md, size_t *len)
103
- {
104
- int_check_digest_has(md, md_block_length);
105
- return md->methods->md_block_length(md, len);
106
- }
107
-
108
- int
109
- krypt_md_name(krypt_md *md, const char **name)
110
- {
111
- int_check_digest_has(md, md_name);
112
- return md->methods->md_name(md, name);
113
- }
114
-
115
- void
116
- krypt_md_mark(krypt_md *md)
117
- {
118
- if (!md) return;
119
- if (md->methods->mark)
120
- md->methods->mark(md);
121
- }
122
-
123
- void
124
- krypt_md_free(krypt_md *md)
125
- {
126
- if (!md) return;
127
- md->methods->free(md);
128
- }
129
-
130
- /** Digest class implementation **/
131
-
132
- VALUE
133
- krypt_digest_new(krypt_md *md)
134
- {
135
- VALUE obj;
136
-
137
- obj = Data_Wrap_Struct(cKryptNativeDigest, krypt_md_mark, krypt_md_free, md);
138
- return obj;
139
- }
140
-
141
- #define int_md_get(obj, md) \
142
- do { \
143
- Data_Get_Struct((obj), krypt_md, (md)); \
144
- if (!(md)) { \
145
- rb_raise(eKryptError, "Uninitialized krypt_md"); \
146
- } \
147
- } while (0)
148
-
149
- /*
150
- * call-seq:
151
- * digest.reset -> self
152
- *
153
- * Resets the Digest in the sense that any Digest#update that has been
154
- * performed is abandoned and the Digest is set to its initial state again.
155
- *
156
- */
157
- static VALUE
158
- krypt_digest_reset(VALUE self)
159
- {
160
- krypt_md *md;
161
-
162
- int_md_get(self, md);
163
- if (krypt_md_reset(md) == KRYPT_ERR) {
164
- rb_raise(eKryptDigestError, "Error while resetting digest");
165
- }
166
- return self;
167
- }
168
-
169
- /*
170
- * call-seq:
171
- * digest.update(string) -> aString
172
- *
173
- * Not every message digest can be computed in one single pass. If a message
174
- * digest is to be computed from several subsequent sources, then each may
175
- * be passed individually to the Digest instance.
176
- *
177
- * === Example
178
- * digest = Krypt::Digest::SHA256.new
179
- * digest.update('First input')
180
- * digest << 'Second input' # equivalent to digest.update('Second input')
181
- * result = digest.digest
182
- *
183
- */
184
- static VALUE
185
- krypt_digest_update(VALUE self, VALUE data)
186
- {
187
- krypt_md *md;
188
- uint8_t *bytes;
189
- size_t len;
190
-
191
- int_md_get(self, md);
192
- StringValue(data);
193
- bytes = (uint8_t *) RSTRING_PTR(data);
194
- len = (size_t) RSTRING_LEN(data);
195
- if (krypt_md_update(md, bytes, len) == KRYPT_ERR) {
196
- rb_raise(eKryptDigestError, "Error while updating digest");
197
- }
198
- return self;
199
- }
200
-
201
- static VALUE
202
- int_digest_data(krypt_md *md, VALUE data)
203
- {
204
- uint8_t *bytes;
205
- size_t len;
206
- uint8_t *digest;
207
- size_t digest_len;
208
- VALUE ret;
209
-
210
- StringValue(data);
211
- bytes = (uint8_t *) RSTRING_PTR(data);
212
- len = (size_t) RSTRING_LEN(data);
213
- if (krypt_md_digest(md, bytes, len, &digest, &digest_len) == KRYPT_ERR) {
214
- rb_raise(eKryptDigestError, "Error while computing digest");
215
- }
216
- ret = rb_str_new((const char *) digest, digest_len);
217
- xfree(digest);
218
- return ret;
219
- }
220
-
221
- static VALUE
222
- int_digest_final(krypt_md *md)
223
- {
224
- uint8_t *digest;
225
- size_t len;
226
- VALUE ret;
227
- if (krypt_md_final(md, &digest, &len) == KRYPT_ERR) {
228
- rb_raise(eKryptDigestError, "Error while finalizing digest");
229
- }
230
- ret = rb_str_new((const char *) digest, len);
231
- xfree(digest);
232
- return ret;
233
- }
234
-
235
- /*
236
- * call-seq:
237
- * digest.digest([string]) -> String
238
- *
239
- * When called with no arguments, the result will be the hash of the data that
240
- * has been fed to this Digest instance so far. If called with a String
241
- * argument, the hash of that argument will be computed.
242
- *
243
- * === Example
244
- * digest = Krypt::Digest::SHA256.new
245
- * result = digest.digest('First input')
246
- *
247
- * is equivalent to
248
- *
249
- * digest = Krypt::Digest::SHA256.new
250
- * digest << 'First input' # equivalent to digest.update('Second input')
251
- * result = digest.digest
252
- *
253
- */
254
- static VALUE
255
- krypt_digest_digest(int argc, VALUE *args, VALUE self)
256
- {
257
- krypt_md *md;
258
- VALUE data;
259
-
260
- rb_scan_args(argc, args, "01", &data);
261
- int_md_get(self, md);
262
- if (!(NIL_P(data)))
263
- return int_digest_data(md, data);
264
- else
265
- return int_digest_final(md);
266
- }
267
-
268
- /*
269
- * call-seq:
270
- * digest.hexdigest([string]) -> String
271
- *
272
- * Works the with the same semantics as Digest#digest with the difference that
273
- * instead of the raw bytes the hex-encoded form of the raw representation is
274
- * returned.
275
- */
276
- static VALUE
277
- krypt_digest_hexdigest(int argc, VALUE *args, VALUE self)
278
- {
279
- VALUE digest, ret;
280
- uint8_t *bytes;
281
- size_t len;
282
-
283
- digest = krypt_digest_digest(argc, args, self);
284
- if (krypt_hex_encode((uint8_t *) RSTRING_PTR(digest), RSTRING_LEN(digest), &bytes, &len) == KRYPT_ERR)
285
- rb_raise(eKryptDigestError, "Error while hex-encoding digest");
286
- ret = rb_str_new((const char *) bytes, len);
287
- xfree(bytes);
288
- return ret;
289
- }
290
-
291
- /*
292
- * call-seq:
293
- * digest.digest_length -> integer
294
- *
295
- * Returns the output size of the digest, i.e. the length in bytes of the
296
- * final message digest result.
297
- *
298
- * === Example
299
- * digest = Krypt::Digest::SHA1.new
300
- * puts digest.digest_length # => 20
301
- *
302
- */
303
- static VALUE
304
- krypt_digest_digest_length(VALUE self)
305
- {
306
- krypt_md *md;
307
- size_t len;
308
-
309
- int_md_get(self, md);
310
- if (krypt_md_digest_length(md, &len) == KRYPT_ERR) {
311
- rb_raise(eKryptDigestError, "Error while getting digest length");
312
- }
313
- return INT2NUM(len);
314
- }
315
-
316
- /*
317
- * call-seq:
318
- * digest.block_length -> integer
319
- *
320
- * Returns the block length of the digest algorithm, i.e. the length in bytes
321
- * of an individual block. Most modern algorithms partition a message to be
322
- * digested into a sequence of fix-sized blocks that are processed
323
- * consecutively.
324
- *
325
- * === Example
326
- * digest = Krypt::Digest::SHA1.new
327
- * puts digest.block_length # => 64
328
- */
329
- static VALUE
330
- krypt_digest_block_length(VALUE self)
331
- {
332
- krypt_md *md;
333
- size_t len;
334
-
335
- int_md_get(self, md);
336
- if (krypt_md_block_length(md, &len) == KRYPT_ERR) {
337
- rb_raise(eKryptDigestError, "Error while getting digest block length");
338
- }
339
- return INT2NUM(len);
340
- }
341
-
342
- /*
343
- * call-seq:
344
- * digest.name -> string
345
- *
346
- * Returns the sn of this Digest instance.
347
- *
348
- * === Example
349
- * digest = Krypt::Digest::SHA512.new
350
- * puts digest.name # => SHA512
351
- *
352
- */
353
- static VALUE
354
- krypt_digest_name(VALUE self)
355
- {
356
- krypt_md *md;
357
- const char *name;
358
-
359
- int_md_get(self, md);
360
- if (krypt_md_name(md, &name) == KRYPT_ERR) {
361
- rb_raise(eKryptDigestError, "Error while getting digest name");
362
- }
363
- return rb_str_new2(name);
364
- }
365
-
366
- void
367
- Init_krypt_digest(void)
368
- {
369
- #if 0
370
- mKrypt = rb_define_module("Krypt"); /* Let RDoc know */
371
- #endif
372
-
373
- mKryptDigest = rb_path2class("Krypt::Digest");
374
- eKryptDigestError = rb_path2class("Krypt::Digest::DigestError");
375
-
376
- /* Document-class: Krypt::Digest::NativeDigest
377
- * Native C implementation of the Krypt::Digest interface.
378
- */
379
- cKryptNativeDigest = rb_define_class_under(mKryptDigest, "NativeDigest", rb_cObject);
380
-
381
- rb_define_method(cKryptNativeDigest, "reset", krypt_digest_reset, 0);
382
- rb_define_method(cKryptNativeDigest, "update", krypt_digest_update, 1);
383
- rb_define_alias(cKryptNativeDigest, "<<", "update");
384
- rb_define_method(cKryptNativeDigest, "digest", krypt_digest_digest, -1);
385
- rb_define_method(cKryptNativeDigest, "hexdigest", krypt_digest_hexdigest, -1);
386
- rb_define_method(cKryptNativeDigest, "digest_length", krypt_digest_digest_length, 0);
387
- rb_define_method(cKryptNativeDigest, "block_length", krypt_digest_block_length, 0);
388
- rb_define_method(cKryptNativeDigest, "name", krypt_digest_name, 0);
389
- rb_undef_method(CLASS_OF(cKryptNativeDigest), "new"); /* private constructor */
390
- }
391
-