sse4rb 1.0.0

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 (4) hide show
  1. data/ext/extconf.rb +3 -0
  2. data/ext/sse.c +377 -0
  3. data/ext/sse.h +78 -0
  4. metadata +48 -0
data/ext/extconf.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile 'sse4rb'
data/ext/sse.c ADDED
@@ -0,0 +1,377 @@
1
+ /*
2
+ ----------------------------------------------------------------------------
3
+ Copyright (c) 2007, Wangxing SHI <swxlion@gmail.com>
4
+
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are met:
9
+
10
+ * Redistributions of source code must retain the above
11
+ copyright notice, this list of conditions and the
12
+ following disclaimer.
13
+
14
+ * Redistributions in binary form must reproduce the above
15
+ copyright notice, this list of conditions and the following
16
+ disclaimer in the documentation and/or other materials
17
+ provided with the distribution.
18
+
19
+ * Neither the copyright holder's name nor the names of its
20
+ contributors may be used to endorse or promote products
21
+ derived from this software without specific prior written
22
+ permission.
23
+
24
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
+ ----------------------------------------------------------------------------
36
+ */
37
+
38
+ /*------------------------------ sse.cpp ----------------------------------*/
39
+ /*===========================================================================
40
+ FILE: sse.cpp
41
+ Author: Wangxing SHI (swxlion)
42
+ Version: 0.0.2
43
+ Date: 2007-10-06
44
+
45
+ File Description:
46
+ Source file of SSE (Simple Stream Encrypt) static library.
47
+
48
+ Dependencies:
49
+ Platform: All platform.
50
+
51
+ History:
52
+ version author data desc
53
+ ----------------------------------------------------
54
+ 0.0.0 swxlion 04-09-04 The first version as code.
55
+ 0.0.1 swxlion 06-10-15 Change the code to encryption.
56
+ 0.0.2 swxlion 07-10-06 Compatible with C language.
57
+ ===========================================================================*/
58
+
59
+ #include <ruby.h>
60
+
61
+ #ifdef __cplusplus
62
+ extern "C"
63
+ {
64
+ #endif /* __cplusplus */
65
+
66
+ /*===========================================================================
67
+
68
+ FUNCTION: sse_encrypt
69
+
70
+ DESCRIPTION:
71
+ Encrypt plaintext. The length of plaintext MUST greater than 1.
72
+
73
+ PARAMETERS:
74
+ ucpBuffer [in, out] - [in] Pointer to the buffer containing the plaintext;
75
+ [out] Pointer to the buffer containing the ciphertext.
76
+ uiBufLen [in] - The plaintext length. In bytes.
77
+ ucpKey [in] - Pointer to the buffer containing the key.
78
+ uiKeylen [in] - The key length. In bytes.
79
+
80
+ RETURN VALUE:
81
+ None.
82
+
83
+ REMARKS:
84
+ The length of ciphertext just is the length of plaintext.
85
+ ===========================================================================*/
86
+ void sse_encrypt( unsigned char * ucpBuffer, const unsigned int uiBufLen, const unsigned char * ucpKey, const unsigned int uiKeylen )
87
+ {
88
+ unsigned char temp11, temp12, temp21, temp22;
89
+ unsigned char key1, key2, skey1, skey2;
90
+ unsigned int i = 0, k = 0, j;
91
+ unsigned int uiCycle = uiBufLen + uiKeylen;
92
+
93
+ if( uiCycle < uiBufLen || uiCycle < uiKeylen )
94
+ {
95
+ uiCycle = (uiBufLen > uiKeylen) ? uiBufLen : uiKeylen;
96
+ }
97
+
98
+ while(uiCycle--)
99
+ {
100
+ key1 = ucpKey[k];
101
+ key2 = ~key1;
102
+
103
+ j = i + 1;
104
+ if( j >= uiBufLen )
105
+ j = 0;
106
+
107
+ if( k + 1 < uiKeylen )
108
+ skey1 = ucpKey[k+1] & 0x07;
109
+ else
110
+ skey1 = ucpKey[0] & 0x07;
111
+ skey2 = key1 & 0x07;
112
+
113
+ ucpBuffer[i] ^= ucpBuffer[j];
114
+ ucpBuffer[j] ^= ucpBuffer[i];
115
+
116
+ temp11 = ucpBuffer[i] & key1;
117
+ temp12 = ucpBuffer[i] & key2;
118
+ temp21 = ucpBuffer[j] & key1;
119
+ temp22 = ucpBuffer[j] & key2;
120
+
121
+ ucpBuffer[i] = temp11 | temp22;
122
+ ucpBuffer[j] = temp21 | temp12;
123
+
124
+ ucpBuffer[i] ^= ucpBuffer[j];
125
+
126
+ temp11 = ucpBuffer[i] << skey1;
127
+ temp12 = ucpBuffer[i] >> ( 8 - skey1 );
128
+ ucpBuffer[i] = temp11 | temp12;
129
+
130
+ temp11 = ucpBuffer[j] << skey2;
131
+ temp12 = ucpBuffer[j] >> ( 8 - skey2 );
132
+ ucpBuffer[j] = temp11 | temp12;
133
+
134
+ ucpBuffer[i] ^= ucpBuffer[j];
135
+
136
+ i += 1;
137
+ k += 1;
138
+ if( i >= uiBufLen )
139
+ {
140
+ i = 0;
141
+ }
142
+ if( k >= uiKeylen )
143
+ {
144
+ k = 0;
145
+ }
146
+ }
147
+ }
148
+
149
+ /*===========================================================================
150
+
151
+ FUNCTION: sse_decrypt
152
+
153
+ DESCRIPTION:
154
+ Decrypt ciphertext. The length of ciphertext MUST greater than 1.
155
+
156
+ PARAMETERS:
157
+ ucpBuffer [in, out] - [in] Pointer to the buffer containing the ciphertext;
158
+ [out] Pointer to the buffer containing the plaintext.
159
+ uiBufLen [in] - The ciphertext length. In bytes.
160
+ ucpKey [in] - Pointer to the buffer containing the key.
161
+ uiKeylen [in] - The key length. In bytes.
162
+
163
+ RETURN VALUE:
164
+ None.
165
+
166
+ REMARKS:
167
+ The length of plaintext just is the length of ciphertext.
168
+ ===========================================================================*/
169
+ void sse_decrypt( unsigned char * ucpBuffer, const unsigned int uiBufLen, const unsigned char * ucpKey, const unsigned int uiKeylen )
170
+ {
171
+ unsigned char temp11, temp12, temp21, temp22;
172
+ unsigned char key1, key2, skey1, skey2;
173
+ unsigned int i = 0, k = 0, j;
174
+ unsigned int uiCycle = uiBufLen + uiKeylen;
175
+
176
+ if( uiCycle < uiBufLen || uiCycle < uiKeylen )
177
+ {
178
+ uiCycle = (uiBufLen > uiKeylen) ? uiBufLen : uiKeylen;
179
+ }
180
+
181
+ /* ���Ҽ��ܽ����� */
182
+ i = uiCycle - 1;
183
+ k = uiCycle - 1;
184
+ while(i >= uiBufLen )
185
+ i -= uiBufLen;
186
+ while(k >= uiKeylen )
187
+ k -= uiKeylen;
188
+
189
+ while(uiCycle--)
190
+ {
191
+ key1 = ucpKey[k];
192
+ key2 = ~key1;
193
+
194
+ j = i + 1;
195
+ if( j >= uiBufLen )
196
+ j = 0;
197
+
198
+ if( k + 1 < uiKeylen )
199
+ skey1 = ucpKey[k+1] & 0x07;
200
+ else
201
+ skey1 = ucpKey[0] & 0x07;
202
+ skey2 = key1 & 0x07;
203
+
204
+ ucpBuffer[i] ^= ucpBuffer[j];
205
+
206
+ temp11 = ucpBuffer[j] << ( 8 - skey2 );
207
+ temp12 = ucpBuffer[j] >> skey2;
208
+ ucpBuffer[j] = temp11 | temp12;
209
+
210
+ temp11 = ucpBuffer[i] << ( 8 - skey1 );
211
+ temp12 = ucpBuffer[i] >> skey1;
212
+ ucpBuffer[i] = temp11 | temp12;
213
+
214
+ ucpBuffer[i] ^= ucpBuffer[j];
215
+
216
+ temp11 = ucpBuffer[i] & key1;
217
+ temp22 = ucpBuffer[i] & key2;
218
+ temp21 = ucpBuffer[j] & key1;
219
+ temp12 = ucpBuffer[j] & key2;
220
+
221
+ ucpBuffer[i] = temp11 | temp12;
222
+ ucpBuffer[j] = temp21 | temp22;
223
+
224
+ ucpBuffer[j] ^= ucpBuffer[i];
225
+ ucpBuffer[i] ^= ucpBuffer[j];
226
+
227
+ if( i == 0 )
228
+ i = uiBufLen - 1;
229
+ else
230
+ i -= 1;
231
+ if( k == 0 )
232
+ k = uiKeylen - 1;
233
+ else
234
+ k -= 1;
235
+ }
236
+ }
237
+
238
+ /*===========================================================================
239
+
240
+ FUNCTION: sse_encrypt_1
241
+
242
+ DESCRIPTION:
243
+ Encrypt plaintext. The length of plaintext MUST equal 1.
244
+
245
+ PARAMETERS:
246
+ ucpBuffer [in, out] - [in] Pointer to the buffer containing the plaintext;
247
+ [out] Pointer to the buffer containing the ciphertext.
248
+ ucpKey [in] - Pointer to the buffer containing the key.
249
+ uiKeylen [in] - The key length. In bytes.
250
+
251
+ RETURN VALUE:
252
+ None.
253
+
254
+ REMARKS:
255
+ The length of ciphertext just is the length of plaintext.
256
+ ===========================================================================*/
257
+ void sse_encrypt_1( unsigned char * ucpBuffer, const unsigned char * ucpKey, const unsigned int uiKeylen )
258
+ {
259
+ unsigned int k = 0;
260
+
261
+ for( k = 0; k < (uiKeylen&~3); k += 4 )
262
+ {
263
+ ucpBuffer[0] ^= ucpKey[k];
264
+ ucpBuffer[0] ^= ucpKey[k+1];
265
+ ucpBuffer[0] ^= ucpKey[k+2];
266
+ ucpBuffer[0] ^= ucpKey[k+3];
267
+ }
268
+
269
+ for( k = (uiKeylen&~3); k < uiKeylen; k++ )
270
+ ucpBuffer[0] ^= ucpKey[k];
271
+ }
272
+
273
+ /*===========================================================================
274
+
275
+ FUNCTION: sse_decrypt_1
276
+
277
+ DESCRIPTION:
278
+ Decrypt ciphertext. The length of ciphertext MUST equal 1.
279
+
280
+ PARAMETERS:
281
+ ucpBuffer [in, out] - [in] Pointer to the buffer containing the ciphertext;
282
+ [out] Pointer to the buffer containing the plaintext.
283
+ ucpKey [in] - Pointer to the buffer containing the key.
284
+ uiKeylen [in] - The key length. In bytes.
285
+
286
+ RETURN VALUE:
287
+ None.
288
+
289
+ REMARKS:
290
+ The length of plaintext just is the length of ciphertext.
291
+ ===========================================================================*/
292
+ void sse_decrypt_1( unsigned char * ucpBuffer, const unsigned char * ucpKey, const unsigned int uiKeylen )
293
+ {
294
+ unsigned int k;
295
+
296
+ for( k = uiKeylen - 1; k >= 1; k-- )
297
+ ucpBuffer[0] ^= ucpKey[k];
298
+
299
+ ucpBuffer[0] ^= ucpKey[0];
300
+ }
301
+
302
+ /*===========================================================================
303
+
304
+ FUNCTION: sse_encrypt_shell
305
+
306
+ DESCRIPTION:
307
+ Shell of SSE algorithm. Encrypt plaintext. The length of plaintext MUST
308
+ greater than 0. This function call sse_encrypt or sse_encrypt_1 inside.
309
+
310
+ PARAMETERS:
311
+ ucpBuffer [in, out] - [in] Pointer to the buffer containing the plaintext;
312
+ [out] Pointer to the buffer containing the ciphertext.
313
+ uiBufLen [in] - The plaintext length. In bytes.
314
+ ucpKey [in] - Pointer to the buffer containing the key.
315
+ uiKeylen [in] - The key length. In bytes.
316
+
317
+ RETURN VALUE:
318
+ None.
319
+
320
+ REMARKS:
321
+ The length of ciphertext just is the length of plaintext.
322
+ ===========================================================================*/
323
+ VALUE sse_encrypt_shell( VALUE self, VALUE ucpBuffer, VALUE ucpKey )
324
+ {
325
+ VALUE str = StringValue(ucpBuffer),str_or = StringValue(ucpKey);
326
+ unsigned char * p=RSTRING_PTR(str),* p_or=RSTRING_PTR(str_or);
327
+ int uiBufLen=RSTRING_LEN(str) , uiKeylen=RSTRING_LEN(str_or);
328
+ if( uiBufLen == 1)
329
+ sse_encrypt_1( p, p_or, uiKeylen );
330
+ else if( uiBufLen != 0 )
331
+ sse_encrypt( p, uiBufLen, p_or, uiKeylen );
332
+ return str;
333
+ }
334
+
335
+ /*===========================================================================
336
+
337
+ FUNCTION: sse_decrypt_shell
338
+
339
+ DESCRIPTION:
340
+ Shell of SSE algorithm. Decrypt ciphertext. The length of ciphertext MUST
341
+ greater than 0. This function call sse_decrypt or sse_decrypt_1 inside.
342
+
343
+ PARAMETERS:
344
+ ucpBuffer [in, out] - [in] Pointer to the buffer containing the ciphertext;
345
+ [out] Pointer to the buffer containing the plaintext.
346
+ uiBufLen [in] - The plaintext length. In bytes.
347
+ ucpKey [in] - Pointer to the buffer containing the key.
348
+ uiKeylen [in] - The key length. In bytes.
349
+
350
+ RETURN VALUE:
351
+ None.
352
+
353
+ REMARKS:
354
+ The length of plaintext just is the length of ciphertext.
355
+ ===========================================================================*/
356
+ VALUE sse_decrypt_shell( VALUE self, VALUE ucpBuffer, VALUE ucpKey )
357
+ {
358
+ VALUE str = StringValue(ucpBuffer),str_or = StringValue(ucpKey);
359
+ unsigned char * p=RSTRING_PTR(str),* p_or=RSTRING_PTR(str_or);
360
+ int uiBufLen=RSTRING_LEN(str) , uiKeylen=RSTRING_LEN(str_or);
361
+ if( uiBufLen == 1)
362
+ sse_decrypt_1( p, p_or, uiKeylen );
363
+ else if( uiBufLen != 0 )
364
+ sse_decrypt( p, uiBufLen, p_or, uiKeylen );
365
+ return str;
366
+ }
367
+
368
+ void Init_sse4rb()
369
+ {
370
+ VALUE sse4rb = rb_define_module("SSE");
371
+ rb_define_module_function(sse4rb, "encrypt", RUBY_METHOD_FUNC(sse_encrypt_shell), 2);
372
+ rb_define_module_function(sse4rb, "decrypt", RUBY_METHOD_FUNC(sse_decrypt_shell), 2);
373
+ }
374
+
375
+ #ifdef __cplusplus
376
+ }
377
+ #endif /* __cplusplus */
data/ext/sse.h ADDED
@@ -0,0 +1,78 @@
1
+ /*
2
+ ----------------------------------------------------------------------------
3
+ Copyright (c) 2007, Wangxing SHI <swxlion@gmail.com>
4
+
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are met:
9
+
10
+ * Redistributions of source code must retain the above
11
+ copyright notice, this list of conditions and the
12
+ following disclaimer.
13
+
14
+ * Redistributions in binary form must reproduce the above
15
+ copyright notice, this list of conditions and the following
16
+ disclaimer in the documentation and/or other materials
17
+ provided with the distribution.
18
+
19
+ * Neither the copyright holder's name nor the names of its
20
+ contributors may be used to endorse or promote products
21
+ derived from this software without specific prior written
22
+ permission.
23
+
24
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
+ ----------------------------------------------------------------------------
36
+ */
37
+
38
+ /*------------------------------- sse.h -----------------------------------*/
39
+ /*===========================================================================
40
+ FILE: sse.h
41
+ Author: Wangxing SHI (swxlion)
42
+ Version: 0.0.2
43
+ Date: 2007-10-06
44
+
45
+ File Description:
46
+ Head file of SSE (Simple Stream Encrypt) static library.
47
+
48
+ Dependencies:
49
+ Platform: All platform.
50
+
51
+ History:
52
+ version author data desc
53
+ ----------------------------------------------------
54
+ 0.0.0 swxlion 04-09-04 The first version as code.
55
+ 0.0.1 swxlion 06-10-15 Change the code to encryption.
56
+ 0.0.2 swxlion 07-10-06 Compatible with C language.
57
+ ===========================================================================*/
58
+
59
+ #ifndef _SSE_H_
60
+ #define _SSE_H_
61
+
62
+ #ifdef __cplusplus
63
+ extern "C"
64
+ {
65
+ #endif /* __cplusplus */
66
+
67
+ void sse_encrypt( unsigned char * ucpBuffer, const unsigned int uiBufLen, const unsigned char * ucpKey, const unsigned int uiKeylen );
68
+ void sse_decrypt( unsigned char * ucpBuffer, const unsigned int uiBufLen, const unsigned char * ucpKey, const unsigned int uiKeylen );
69
+ void sse_encrypt_1( unsigned char * ucpBuffer, const unsigned char * ucpKey, const unsigned int uiKeylen );
70
+ void sse_decrypt_1( unsigned char * ucpBuffer, const unsigned char * ucpKey, const unsigned int uiKeylen );
71
+ void sse_encrypt_shell( unsigned char * ucpBuffer, const unsigned int uiBufLen, const unsigned char * ucpKey, const unsigned int uiKeylen );
72
+ void sse_decrypt_shell( unsigned char * ucpBuffer, const unsigned int uiBufLen, const unsigned char * ucpKey, const unsigned int uiKeylen );
73
+
74
+ #ifdef __cplusplus
75
+ }
76
+ #endif /* __cplusplus */
77
+
78
+ #endif /* _SSE_H_ */
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sse4rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Parnado
9
+ autorequire: sse4rb
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ executables: []
17
+ extensions:
18
+ - ext/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ext/sse.h
22
+ - ext/sse.c
23
+ - ext/extconf.rb
24
+ homepage:
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - .
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.11
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: SSE for Ruby.
48
+ test_files: []