facets 2.6.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.rdoc +331 -35
- data/MANIFEST +685 -826
- data/{doc/guide/notes.rd → NOTES} +0 -0
- data/README.rdoc +73 -25
- data/Rakefile +245 -2
- data/TODO +5 -0
- data/demo/hook.rd +47 -0
- data/demo/scenario_require.rd +9 -0
- data/doc/README.more +24 -6
- data/doc/manual/about.rb +47 -0
- data/doc/manual/annotations.rdoc +60 -0
- data/doc/manual/associations.rdoc +55 -0
- data/doc/manual/blockups.rdoc +101 -0
- data/doc/manual/capsule.rdoc +34 -0
- data/doc/manual/command.rdoc +177 -0
- data/doc/manual/core.rdoc +37 -0
- data/doc/manual/faq.rdoc +32 -0
- data/doc/manual/typecast.html +112 -0
- data/lib/core/facets.rb +359 -11
- data/lib/core/facets/array/combination.rb +3 -3
- data/lib/core/facets/array/index.rb +4 -1
- data/lib/core/facets/array/permutation.rb +2 -2
- data/lib/core/facets/array/product.rb +1 -1
- data/lib/core/facets/binding/eval.rb +1 -1
- data/lib/core/facets/denumerable.rb +76 -0
- data/lib/core/facets/duplicable.rb +34 -0
- data/lib/core/facets/enumerable/count.rb +10 -4
- data/lib/core/facets/enumerable/defer.rb +77 -0
- data/lib/core/facets/enumerable/each_by.rb +1 -1
- data/lib/core/facets/enumerable/every.rb +35 -0
- data/lib/{more/facets/elementwise.rb → core/facets/enumerable/ewise.rb} +0 -0
- data/lib/core/facets/enumerable/filter.rb +25 -0
- data/lib/core/facets/enumerable/group_by.rb +1 -1
- data/lib/core/facets/enumerable/none.rb +3 -2
- data/lib/core/facets/enumerable/one.rb +3 -2
- data/lib/core/facets/enumerable/per.rb +61 -0
- data/lib/core/facets/exception/raised.rb +14 -0
- data/lib/core/facets/integer/odd.rb +5 -1
- data/lib/core/facets/kernel/__dir__.rb +13 -3
- data/lib/core/facets/kernel/__here__.rb +14 -0
- data/lib/core/facets/kernel/__method__.rb +9 -3
- data/lib/core/facets/kernel/ask.rb +1 -0
- data/lib/core/facets/kernel/equate.rb +13 -0
- data/lib/core/facets/kernel/extension.rb +9 -0
- data/lib/core/facets/kernel/identical.rb +4 -0
- data/lib/core/facets/kernel/instance_exec.rb +2 -1
- data/lib/core/facets/kernel/method.rb +49 -0
- data/lib/core/facets/kernel/object_send.rb +2 -2
- data/lib/core/facets/kernel/{state.rb → object_state.rb} +23 -12
- data/lib/core/facets/kernel/require_all.rb +6 -1
- data/lib/core/facets/kernel/require_local.rb +8 -1
- data/lib/core/facets/kernel/require_relative.rb +52 -0
- data/lib/core/facets/kernel/source_location.rb +13 -0
- data/lib/core/facets/kernel/tap.rb +13 -6
- data/lib/core/facets/module/attr_setter.rb +57 -0
- data/lib/core/facets/module/instance_method.rb +24 -0
- data/lib/core/facets/module/module_load.rb +60 -44
- data/lib/core/facets/module/module_require.rb +1 -0
- data/lib/core/facets/nilclass/to_f.rb +1 -1
- data/lib/core/facets/objectspace/op_fetch.rb +3 -0
- data/lib/core/facets/proc/curry.rb +4 -3
- data/lib/core/facets/string/bytes.rb +10 -4
- data/lib/core/facets/string/camelcase.rb +6 -5
- data/lib/core/facets/string/chars.rb +5 -1
- data/lib/core/facets/string/each_char.rb +1 -1
- data/lib/core/facets/string/each_word.rb +1 -1
- data/lib/core/facets/string/lines.rb +11 -4
- data/lib/core/facets/string/start_with.rb +9 -2
- data/lib/core/facets/string/unfold.rb +27 -0
- data/lib/core/facets/symbol/succ.rb +3 -3
- data/lib/core/facets/symbol/thrown.rb +20 -0
- data/lib/core/facets/symbol/to_proc.rb +3 -2
- data/lib/core/facets/time/to_time.rb +1 -1
- data/lib/core/facets/to_hash.rb +41 -100
- data/lib/core/facets/unboundmethod/name.rb +20 -23
- data/lib/more/facets/ansicode.rb +1 -10
- data/lib/more/facets/autoarray.rb +3 -31
- data/lib/more/facets/basicobject.rb +73 -0
- data/lib/more/facets/blankslate.rb +2 -66
- data/lib/{lore → more}/facets/cgi.rb +0 -0
- data/lib/more/facets/class_extend.rb +1 -0
- data/lib/{lore → more}/facets/continuation.rb +0 -0
- data/lib/{lore → more}/facets/date.rb +3 -3
- data/lib/more/facets/enumargs.rb +192 -0
- data/lib/more/facets/enumerablepass.rb +2 -216
- data/lib/more/facets/enumerator.rb +62 -0
- data/lib/more/facets/{equatable.rb → equitable.rb} +11 -11
- data/lib/more/facets/expirable.rb +13 -41
- data/lib/{lore → more}/facets/fileutils.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/head.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/safe_ln.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/slice.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/tail.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/wc.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/whereis.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/which.rb +0 -0
- data/lib/{lore → more}/facets/getoptlong.rb +0 -0
- data/lib/more/facets/hook.rb +2 -29
- data/lib/more/facets/inheritor.rb +2 -2
- data/lib/more/facets/instance_eval.rb +50 -0
- data/lib/more/facets/instance_function.rb +78 -0
- data/lib/more/facets/main.rb +20 -15
- data/lib/more/facets/memoize.rb +1 -113
- data/lib/more/facets/module/attr.rb +83 -0
- data/lib/more/facets/module/attr_tester.rb +44 -0
- data/lib/more/facets/module/attr_toggler.rb +59 -0
- data/lib/more/facets/module/attr_validator.rb +34 -0
- data/lib/more/facets/{class_extension.rb → module/class_extend.rb} +21 -13
- data/lib/more/facets/once.rb +59 -0
- data/lib/more/facets/openmodule.rb +1 -0
- data/lib/more/facets/orderedhash.rb +1 -33
- data/lib/{lore → more}/facets/ostruct.rb +0 -0
- data/lib/more/facets/ostructable.rb +1 -4
- data/lib/more/facets/partial.rb +18 -16
- data/lib/{lore → more}/facets/pathname.rb +0 -0
- data/lib/more/facets/preinitialize.rb +157 -0
- data/lib/{lore → more}/facets/rbconfig.rb +0 -0
- data/lib/more/facets/recorder.rb +1 -2
- data/lib/{lore → more}/facets/set.rb +0 -0
- data/lib/{lore → more}/facets/shellwords.rb +0 -0
- data/lib/{lore → more}/facets/uri.rb +0 -0
- data/lib/{lore → more}/facets/yaml.rb +0 -0
- data/lib/{lore → more}/facets/zlib.rb +0 -0
- data/meta/loadpath +0 -1
- data/meta/sitemap +4 -0
- data/meta/version +1 -1
- data/test/core/enumerable/test_count.rb +1 -1
- data/test/{more/test_filter.rb → core/enumerable/test_defer.rb} +24 -22
- data/test/{more/test_elementor.rb → core/enumerable/test_every.rb} +2 -15
- data/test/core/enumerable/test_ewise.rb +23 -0
- data/test/core/enumerable/test_per.rb +18 -0
- data/test/core/enumerable/test_take.rb +13 -0
- data/test/core/kernel/test_deepcopy.rb +1 -1
- data/test/{more/test_1stclassmethod.rb → core/kernel/test_method.rb} +2 -7
- data/test/core/kernel/test_tap.rb +1 -1
- data/test/core/proc/test_curry.rb +11 -0
- data/test/core/string/test_bytes.rb +1 -1
- data/test/core/string/test_camelcase.rb +23 -6
- data/test/core/string/test_lines.rb +1 -1
- data/test/core/string/test_unfold.rb +14 -0
- data/test/{more → core}/test_blank.rb +0 -0
- data/test/{more → core}/test_boolean.rb +0 -0
- data/test/{more → core}/test_functor.rb +0 -0
- data/test/{lore → more}/test_basicobject.rb +0 -0
- data/test/more/{test_class_extension.rb → test_class_extend.rb} +6 -6
- data/test/{lore → more}/test_continuation.rb +0 -0
- data/test/{lore → more}/test_date.rb +0 -0
- data/test/more/{test_enumerablepass.rb → test_enumargs.rb} +2 -4
- data/test/more/{test_equatable.rb → test_equitable.rb} +2 -2
- data/test/more/{test_instantise.rb → test_instance_function.rb} +3 -2
- data/test/more/test_memoize.rb +1 -1
- data/test/{lore → more}/test_ostruct.rb +0 -0
- metadata +865 -1016
- data/RELEASE +0 -38
- data/doc/README.lore +0 -51
- data/doc/log/basic_stats/index.html +0 -39
- data/doc/log/changelog.html +0 -648
- data/doc/log/changelog.txt +0 -217
- data/doc/log/stats/index.html +0 -39
- data/doc/log/testlog.txt +0 -278
- data/doc/notes/CHANGES +0 -2529
- data/doc/rdoc/lore/classes/Array.html +0 -176
- data/doc/rdoc/lore/classes/CGI.html +0 -191
- data/doc/rdoc/lore/classes/Config.html +0 -135
- data/doc/rdoc/lore/classes/Continuation.html +0 -113
- data/doc/rdoc/lore/classes/Date.html +0 -631
- data/doc/rdoc/lore/classes/DateTime.html +0 -583
- data/doc/rdoc/lore/classes/Enumerable.html +0 -89
- data/doc/rdoc/lore/classes/Enumerable/Enumerator.html +0 -147
- data/doc/rdoc/lore/classes/File.html +0 -128
- data/doc/rdoc/lore/classes/FileUtils.html +0 -434
- data/doc/rdoc/lore/classes/GetoptLong.html +0 -118
- data/doc/rdoc/lore/classes/GetoptLong/DSL.html +0 -208
- data/doc/rdoc/lore/classes/Kernel.html +0 -135
- data/doc/rdoc/lore/classes/Logger.html +0 -229
- data/doc/rdoc/lore/classes/Logger/Ansicolor.html +0 -277
- data/doc/rdoc/lore/classes/Logger/LogDevice.html +0 -121
- data/doc/rdoc/lore/classes/NilClass.html +0 -119
- data/doc/rdoc/lore/classes/OpenStruct.html +0 -432
- data/doc/rdoc/lore/classes/Pathname.html +0 -353
- data/doc/rdoc/lore/classes/Set.html +0 -117
- data/doc/rdoc/lore/classes/Shellwords.html +0 -111
- data/doc/rdoc/lore/classes/String.html +0 -140
- data/doc/rdoc/lore/classes/Time.html +0 -154
- data/doc/rdoc/lore/classes/URI.html +0 -454
- data/doc/rdoc/lore/classes/URI/Hash.html +0 -105
- data/doc/rdoc/lore/classes/URI/Kernel.html +0 -122
- data/doc/rdoc/lore/classes/Zlib.html +0 -188
- data/doc/rdoc/lore/created.rid +0 -1
- data/doc/rdoc/lore/files/README.html +0 -286
- data/doc/rdoc/lore/files/doc/README_lore.html +0 -155
- data/doc/rdoc/lore/files/lib/lore/facets/basicobject_rb.html +0 -118
- data/doc/rdoc/lore/files/lib/lore/facets/cgi_rb.html +0 -111
- data/doc/rdoc/lore/files/lib/lore/facets/continuation_rb.html +0 -147
- data/doc/rdoc/lore/files/lib/lore/facets/date_rb.html +0 -97
- data/doc/rdoc/lore/files/lib/lore/facets/enumerator_rb.html +0 -111
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/head_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/safe_ln_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/slice_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/tail_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/wc_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/whereis_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/which_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils_rb.html +0 -131
- data/doc/rdoc/lore/files/lib/lore/facets/getoptlong_rb.html +0 -135
- data/doc/rdoc/lore/files/lib/lore/facets/logger_rb.html +0 -142
- data/doc/rdoc/lore/files/lib/lore/facets/ostruct_rb.html +0 -135
- data/doc/rdoc/lore/files/lib/lore/facets/pathname_rb.html +0 -145
- data/doc/rdoc/lore/files/lib/lore/facets/rbconfig_rb.html +0 -124
- data/doc/rdoc/lore/files/lib/lore/facets/set_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/shellwords_rb.html +0 -124
- data/doc/rdoc/lore/files/lib/lore/facets/uri_rb.html +0 -125
- data/doc/rdoc/lore/files/lib/lore/facets/yaml_rb.html +0 -146
- data/doc/rdoc/lore/files/lib/lore/facets/zlib_rb.html +0 -97
- data/doc/rdoc/lore/fr_class_index.html +0 -73
- data/doc/rdoc/lore/fr_file_index.html +0 -71
- data/doc/rdoc/lore/fr_method_index.html +0 -177
- data/doc/rdoc/lore/index.html +0 -26
- data/doc/rdoc/lore/rdoc-style.css +0 -177
- data/doc/release-notes/RELEASE-2.0.5 +0 -8
- data/doc/release-notes/RELEASE-2.1.0 +0 -9
- data/doc/release-notes/RELEASE-2.1.1 +0 -5
- data/doc/release-notes/RELEASE-2.1.2 +0 -6
- data/doc/release-notes/RELEASE-2.1.3 +0 -5
- data/doc/release-notes/RELEASE-2.2.0 +0 -14
- data/doc/release-notes/RELEASE-2.2.1 +0 -4
- data/doc/release-notes/RELEASE-2.3.0 +0 -6
- data/doc/release-notes/RELEASE-2.4.0 +0 -70
- data/doc/release-notes/RELEASE-2.4.1 +0 -8
- data/doc/release-notes/RELEASE-2.4.2 +0 -4
- data/doc/release-notes/RELEASE-2.4.3 +0 -78
- data/doc/release-notes/RELEASE-2.4.4 +0 -38
- data/doc/release-notes/RELEASE-2.4.5 +0 -37
- data/doc/release-notes/RELEASE-2.5.0 +0 -83
- data/lib/core/facets/kernel/instance.rb +0 -19
- data/lib/lore/facets/basicobject.rb +0 -14
- data/lib/lore/facets/enumerator.rb +0 -67
- data/lib/lore/facets/logger.rb +0 -291
- data/lib/more/facets/1stclassmethod.rb +0 -140
- data/lib/more/facets/advisable.rb +0 -162
- data/lib/more/facets/association.rb +0 -210
- data/lib/more/facets/attr.rb +0 -209
- data/lib/more/facets/basex.rb +0 -37
- data/lib/more/facets/bbcode.rb +0 -397
- data/lib/more/facets/bicrypt.rb +0 -265
- data/lib/more/facets/binreadable.rb +0 -221
- data/lib/more/facets/censor.rb +0 -97
- data/lib/more/facets/classmethods.rb +0 -199
- data/lib/more/facets/consoleutils.rb +0 -99
- data/lib/more/facets/crypt.rb +0 -166
- data/lib/more/facets/dependency.rb +0 -151
- data/lib/more/facets/downloader.rb +0 -281
- data/lib/more/facets/duplicable.rb +0 -43
- data/lib/more/facets/elementor.rb +0 -133
- data/lib/more/facets/filter.rb +0 -121
- data/lib/more/facets/heap.rb +0 -22
- data/lib/more/facets/infinity.rb +0 -193
- data/lib/more/facets/ini.rb +0 -264
- data/lib/more/facets/instantise.rb +0 -1
- data/lib/more/facets/instantize.rb +0 -95
- data/lib/more/facets/interval.rb +0 -282
- data/lib/more/facets/iteration.rb +0 -65
- data/lib/more/facets/linkedlist.rb +0 -222
- data/lib/more/facets/lrucache.rb +0 -157
- data/lib/more/facets/matcher.rb +0 -140
- data/lib/more/facets/memoizer.rb +0 -74
- data/lib/more/facets/minitar.rb +0 -1063
- data/lib/more/facets/nackclass.rb +0 -41
- data/lib/more/facets/net/smtp_tls.rb +0 -131
- data/lib/more/facets/nilstatus.rb +0 -48
- data/lib/more/facets/overload.rb +0 -94
- data/lib/more/facets/paramix.rb +0 -202
- data/lib/more/facets/pool.rb +0 -91
- data/lib/more/facets/pqueue.rb +0 -449
- data/lib/more/facets/pry.rb +0 -32
- data/lib/more/facets/reflection.rb +0 -145
- data/lib/more/facets/semaphore.rb +0 -92
- data/lib/more/facets/settings.rb +0 -248
- data/lib/more/facets/snapshot.rb +0 -209
- data/lib/more/facets/sparse_array.rb +0 -809
- data/lib/more/facets/string/mask.rb +0 -278
- data/lib/more/facets/string/obfuscate.rb +0 -65
- data/lib/more/facets/string/stylize.rb +0 -169
- data/lib/more/facets/string/words.rb +0 -167
- data/lib/more/facets/syncarray.rb +0 -114
- data/lib/more/facets/synchash.rb +0 -157
- data/lib/more/facets/typecast.rb +0 -261
- data/lib/more/facets/uninheritable.rb +0 -50
- data/lib/more/facets/xmlhash.rb +0 -112
- data/lib/more/facets/xoxo.rb +0 -259
- data/lib/more/facets/ziputils.rb +0 -490
- data/task/conflicts +0 -63
- data/task/coverage.rake +0 -37
- data/task/methods +0 -49
- data/task/rdoc.rake +0 -17
- data/task/setup.rake +0 -38
- data/task/test.rake +0 -145
- data/test/more/test_advisable.rb +0 -71
- data/test/more/test_association.rb +0 -38
- data/test/more/test_bbcode.rb +0 -21
- data/test/more/test_binreadable.rb +0 -50
- data/test/more/test_buildable.rb +0 -73
- data/test/more/test_classmethods.rb +0 -56
- data/test/more/test_crypt.rb +0 -32
- data/test/more/test_dependency.rb +0 -69
- data/test/more/test_elementwise.rb +0 -25
- data/test/more/test_infinity.rb +0 -40
- data/test/more/test_interval.rb +0 -151
- data/test/more/test_linkedlist.rb +0 -41
- data/test/more/test_lrucache.rb +0 -14
- data/test/more/test_overload.rb +0 -160
- data/test/more/test_paramix.rb +0 -170
- data/test/more/test_prototype.rb +0 -35
- data/test/more/test_snapshot.rb +0 -21
- data/test/more/test_sparsearray.rb +0 -279
- data/test/more/test_syncarray.rb +0 -15
- data/test/more/test_synchash.rb +0 -16
- data/test/more/test_typecast.rb +0 -54
- data/test/more/test_uninheritable.rb +0 -31
- data/test/more/test_xoxo.rb +0 -274
- data/test/test_facets.rb +0 -9
data/lib/more/facets/bicrypt.rb
DELETED
@@ -1,265 +0,0 @@
|
|
1
|
-
# = BiCrypt
|
2
|
-
#
|
3
|
-
# A simple two-way encryption class.
|
4
|
-
#
|
5
|
-
# == Authors
|
6
|
-
#
|
7
|
-
# * Trans
|
8
|
-
#
|
9
|
-
# == Copying
|
10
|
-
#
|
11
|
-
# Copyright (c) 2007 Trans
|
12
|
-
#
|
13
|
-
# Ruby License
|
14
|
-
#
|
15
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
16
|
-
# software under the same terms as Ruby.
|
17
|
-
#
|
18
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
19
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
20
|
-
# FOR A PARTICULAR PURPOSE.
|
21
|
-
|
22
|
-
require 'stringio'
|
23
|
-
require 'facets/string/xor'
|
24
|
-
|
25
|
-
# = BiCrypt
|
26
|
-
#
|
27
|
-
# A simple two-way encryption class.
|
28
|
-
#
|
29
|
-
class BiCrypt
|
30
|
-
|
31
|
-
ULONG = 0x100000000
|
32
|
-
|
33
|
-
def block_size
|
34
|
-
return(8)
|
35
|
-
end
|
36
|
-
|
37
|
-
def initialize(userKey)
|
38
|
-
|
39
|
-
# These are the S-boxes given in Applied Cryptography 2nd Ed., p. 333
|
40
|
-
@sBox = [
|
41
|
-
[4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3],
|
42
|
-
[14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9],
|
43
|
-
[5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11],
|
44
|
-
[7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3],
|
45
|
-
[6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2],
|
46
|
-
[4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14],
|
47
|
-
[13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12],
|
48
|
-
[1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12]
|
49
|
-
]
|
50
|
-
|
51
|
-
# These are the S-boxes given in the GOST source code listing in Applied
|
52
|
-
# Cryptography 2nd Ed., p. 644. They appear to be from the DES S-boxes
|
53
|
-
# [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 ],
|
54
|
-
# [ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 ],
|
55
|
-
# [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 ],
|
56
|
-
# [ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 ],
|
57
|
-
# [ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 ],
|
58
|
-
# [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 ],
|
59
|
-
# [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 ],
|
60
|
-
# [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 ]
|
61
|
-
|
62
|
-
# precalculate the S table
|
63
|
-
@sTable = precalculate_S_table()
|
64
|
-
|
65
|
-
# derive the 32-byte key from the user-supplied key
|
66
|
-
userKeyLength = userKey.length
|
67
|
-
@key = userKey[0..31].unpack('C'*32)
|
68
|
-
if (userKeyLength < 32)
|
69
|
-
userKeyLength.upto(31) { @key << 0 }
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
|
74
|
-
def precalculate_S_table()
|
75
|
-
sTable = [[], [], [], []]
|
76
|
-
0.upto(3) { |i|
|
77
|
-
0.upto(255) { |j|
|
78
|
-
t = @sBox[2*i][j % 16] | (@sBox[2*i+1][j/16] << 4)
|
79
|
-
u = (8*i + 11) % 32
|
80
|
-
v = (t << u) | (t >> (32-u))
|
81
|
-
sTable[i][j] = (v % ULONG)
|
82
|
-
}
|
83
|
-
}
|
84
|
-
return(sTable)
|
85
|
-
end
|
86
|
-
|
87
|
-
|
88
|
-
def f(longWord)
|
89
|
-
longWord = longWord % ULONG
|
90
|
-
a, b, c, d = [longWord].pack('L').unpack('CCCC')
|
91
|
-
return(@sTable[3][d] ^ @sTable[2][c] ^ @sTable[1][b] ^ @sTable[0][a])
|
92
|
-
end
|
93
|
-
|
94
|
-
def encrypt_pair(xl, xr)
|
95
|
-
3.times {
|
96
|
-
xr ^= f(xl+@key[0])
|
97
|
-
xl ^= f(xr+@key[1])
|
98
|
-
xr ^= f(xl+@key[2])
|
99
|
-
xl ^= f(xr+@key[3])
|
100
|
-
xr ^= f(xl+@key[4])
|
101
|
-
xl ^= f(xr+@key[5])
|
102
|
-
xr ^= f(xl+@key[6])
|
103
|
-
xl ^= f(xr+@key[7])
|
104
|
-
}
|
105
|
-
xr ^= f(xl+@key[7])
|
106
|
-
xl ^= f(xr+@key[6])
|
107
|
-
xr ^= f(xl+@key[5])
|
108
|
-
xl ^= f(xr+@key[4])
|
109
|
-
xr ^= f(xl+@key[3])
|
110
|
-
xl ^= f(xr+@key[2])
|
111
|
-
xr ^= f(xl+@key[1])
|
112
|
-
xl ^= f(xr+@key[0])
|
113
|
-
return([xr, xl])
|
114
|
-
end
|
115
|
-
|
116
|
-
|
117
|
-
def decrypt_pair(xl, xr)
|
118
|
-
xr ^= f(xl+@key[0])
|
119
|
-
xl ^= f(xr+@key[1])
|
120
|
-
xr ^= f(xl+@key[2])
|
121
|
-
xl ^= f(xr+@key[3])
|
122
|
-
xr ^= f(xl+@key[4])
|
123
|
-
xl ^= f(xr+@key[5])
|
124
|
-
xr ^= f(xl+@key[6])
|
125
|
-
xl ^= f(xr+@key[7])
|
126
|
-
3.times {
|
127
|
-
xr ^= f(xl+@key[7])
|
128
|
-
xl ^= f(xr+@key[6])
|
129
|
-
xr ^= f(xl+@key[5])
|
130
|
-
xl ^= f(xr+@key[4])
|
131
|
-
xr ^= f(xl+@key[3])
|
132
|
-
xl ^= f(xr+@key[2])
|
133
|
-
xr ^= f(xl+@key[1])
|
134
|
-
xl ^= f(xr+@key[0])
|
135
|
-
}
|
136
|
-
return([xr, xl])
|
137
|
-
end
|
138
|
-
|
139
|
-
def encrypt_block(block)
|
140
|
-
xl, xr = block.unpack('NN')
|
141
|
-
xl, xr = encrypt_pair(xl, xr)
|
142
|
-
encrypted = [xl, xr].pack('NN')
|
143
|
-
return(encrypted)
|
144
|
-
end
|
145
|
-
|
146
|
-
|
147
|
-
def decrypt_block(block)
|
148
|
-
xl, xr = block.unpack('NN')
|
149
|
-
xl, xr = decrypt_pair(xl, xr)
|
150
|
-
decrypted = [xl, xr].pack('NN')
|
151
|
-
return(decrypted)
|
152
|
-
end
|
153
|
-
|
154
|
-
# When this module is mixed in with an encryption class, the class
|
155
|
-
# must provide three methods: encrypt_block(block) and decrypt_block(block)
|
156
|
-
# and block_size()
|
157
|
-
|
158
|
-
def generate_initialization_vector(words)
|
159
|
-
srand(Time.now.to_i)
|
160
|
-
vector = ""
|
161
|
-
words.times {
|
162
|
-
vector << [rand(ULONG)].pack('N')
|
163
|
-
}
|
164
|
-
return(vector)
|
165
|
-
end
|
166
|
-
|
167
|
-
|
168
|
-
def encrypt_stream(plainStream, cryptStream)
|
169
|
-
# Cypher-block-chain mode
|
170
|
-
|
171
|
-
initVector = generate_initialization_vector(block_size() / 4)
|
172
|
-
chain = encrypt_block(initVector)
|
173
|
-
cryptStream.write(chain)
|
174
|
-
|
175
|
-
while ((block = plainStream.read(block_size())) && (block.length == block_size()))
|
176
|
-
block = block ^ chain
|
177
|
-
encrypted = encrypt_block(block)
|
178
|
-
cryptStream.write(encrypted)
|
179
|
-
chain = encrypted
|
180
|
-
end
|
181
|
-
|
182
|
-
# write the final block
|
183
|
-
# At most block_size()-1 bytes can be part of the message.
|
184
|
-
# That means the final byte can be used to store the number of meaningful
|
185
|
-
# bytes in the final block
|
186
|
-
block = '' if block.nil?
|
187
|
-
buffer = block.split('')
|
188
|
-
remainingMessageBytes = buffer.length
|
189
|
-
# we use 7-bit characters to avoid possible strange behavior on the Mac
|
190
|
-
remainingMessageBytes.upto(block_size()-2) { buffer << rand(128).chr }
|
191
|
-
buffer << remainingMessageBytes.chr
|
192
|
-
block = buffer.join('')
|
193
|
-
block = block ^ chain
|
194
|
-
encrypted = encrypt_block(block)
|
195
|
-
cryptStream.write(encrypted)
|
196
|
-
end
|
197
|
-
|
198
|
-
|
199
|
-
def decrypt_stream(cryptStream, plainStream)
|
200
|
-
# Cypher-block-chain mode
|
201
|
-
chain = cryptStream.read(block_size())
|
202
|
-
|
203
|
-
while (block = cryptStream.read(block_size()))
|
204
|
-
decrypted = decrypt_block(block)
|
205
|
-
plainText = decrypted ^ chain
|
206
|
-
plainStream.write(plainText) unless cryptStream.eof?
|
207
|
-
chain = block
|
208
|
-
end
|
209
|
-
|
210
|
-
# write the final block, omitting the padding
|
211
|
-
buffer = plainText.split('')
|
212
|
-
remainingMessageBytes = buffer.last.unpack('C').first
|
213
|
-
remainingMessageBytes.times { plainStream.write(buffer.shift) }
|
214
|
-
end
|
215
|
-
|
216
|
-
|
217
|
-
def carefully_open_file(filename, mode)
|
218
|
-
begin
|
219
|
-
aFile = File.new(filename, mode)
|
220
|
-
rescue
|
221
|
-
puts "Sorry. There was a problem opening the file <#{filename}>."
|
222
|
-
aFile.close() unless aFile.nil?
|
223
|
-
raise
|
224
|
-
end
|
225
|
-
return(aFile)
|
226
|
-
end
|
227
|
-
|
228
|
-
|
229
|
-
def encrypt_file(plainFilename, cryptFilename)
|
230
|
-
plainFile = carefully_open_file(plainFilename, 'rb')
|
231
|
-
cryptFile = carefully_open_file(cryptFilename, 'wb+')
|
232
|
-
encrypt_stream(plainFile, cryptFile)
|
233
|
-
plainFile.close unless plainFile.closed?
|
234
|
-
cryptFile.close unless cryptFile.closed?
|
235
|
-
end
|
236
|
-
|
237
|
-
|
238
|
-
def decrypt_file(cryptFilename, plainFilename)
|
239
|
-
cryptFile = carefully_open_file(cryptFilename, 'rb')
|
240
|
-
plainFile = carefully_open_file(plainFilename, 'wb+')
|
241
|
-
decrypt_stream(cryptFile, plainFile)
|
242
|
-
cryptFile.close unless cryptFile.closed?
|
243
|
-
plainFile.close unless plainFile.closed?
|
244
|
-
end
|
245
|
-
|
246
|
-
|
247
|
-
def encrypt_string(plainText)
|
248
|
-
plainStream = StringIO.new(plainText)
|
249
|
-
cryptStream = StringIO.new('')
|
250
|
-
encrypt_stream(plainStream, cryptStream)
|
251
|
-
cryptText = cryptStream.string
|
252
|
-
return(cryptText)
|
253
|
-
end
|
254
|
-
|
255
|
-
|
256
|
-
def decrypt_string(cryptText)
|
257
|
-
cryptStream = StringIO.new(cryptText)
|
258
|
-
plainStream = StringIO.new('')
|
259
|
-
decrypt_stream(cryptStream, plainStream)
|
260
|
-
plainText = plainStream.string
|
261
|
-
return(plainText)
|
262
|
-
end
|
263
|
-
|
264
|
-
end
|
265
|
-
|
@@ -1,221 +0,0 @@
|
|
1
|
-
# = BinReadable
|
2
|
-
#
|
3
|
-
# This mixin solely depends on method read(n), which must be
|
4
|
-
# defined in the class/module where you mix in this module.
|
5
|
-
#
|
6
|
-
# == Todo
|
7
|
-
#
|
8
|
-
# * The name of this module sucks.
|
9
|
-
#
|
10
|
-
# == Authors
|
11
|
-
#
|
12
|
-
# * Michael Neumann
|
13
|
-
#
|
14
|
-
# == Copying
|
15
|
-
#
|
16
|
-
# Copyright (c) 2003 Michael Neumann
|
17
|
-
#
|
18
|
-
# Ruby License
|
19
|
-
#
|
20
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
21
|
-
# software under the same terms as Ruby.
|
22
|
-
#
|
23
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
24
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
25
|
-
# FOR A PARTICULAR PURPOSE.
|
26
|
-
|
27
|
-
# = BinReadable
|
28
|
-
#
|
29
|
-
# This mixin solely depends on method read(n), which must be
|
30
|
-
# defined in the class/module where you mix in this module.
|
31
|
-
|
32
|
-
module BinReadable
|
33
|
-
|
34
|
-
#--
|
35
|
-
# TODO Would like to get the core functionality this provides into the
|
36
|
-
# System module and then change BinaryReader to depend on that instead.
|
37
|
-
#++
|
38
|
-
module ByteOrder
|
39
|
-
|
40
|
-
Native = :Native
|
41
|
-
BigEndian = Big = Network = :BigEndian
|
42
|
-
LittleEndian = Little = :LittleEndian
|
43
|
-
|
44
|
-
# examines the byte order of the underlying machine
|
45
|
-
def byte_order
|
46
|
-
if [0x12345678].pack("L") == "\x12\x34\x56\x78"
|
47
|
-
BigEndian
|
48
|
-
else
|
49
|
-
LittleEndian
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
alias_method :byteorder, :byte_order
|
54
|
-
|
55
|
-
def little_endian?
|
56
|
-
byte_order == LittleEndian
|
57
|
-
end
|
58
|
-
|
59
|
-
def big_endian?
|
60
|
-
byte_order == BigEndian
|
61
|
-
end
|
62
|
-
|
63
|
-
alias_method :little?, :little_endian?
|
64
|
-
alias_method :big?, :big_endian?
|
65
|
-
alias_method :network?, :big_endian?
|
66
|
-
|
67
|
-
module_function :byte_order, :byteorder
|
68
|
-
module_function :little_endian?, :little?
|
69
|
-
module_function :big_endian?, :big?, :network?
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
# default is native byte-order
|
74
|
-
def byte_order
|
75
|
-
@byte_order || ByteOrder::Native
|
76
|
-
end
|
77
|
-
|
78
|
-
def byte_order=(new_byteorder)
|
79
|
-
@byte_order = new_byteorder
|
80
|
-
end
|
81
|
-
|
82
|
-
alias byteorder byte_order
|
83
|
-
alias byteorder= byte_order=
|
84
|
-
|
85
|
-
# == 8 bit
|
86
|
-
|
87
|
-
# no byteorder for 8 bit!
|
88
|
-
|
89
|
-
def read_word8
|
90
|
-
ru(1, 'C')
|
91
|
-
end
|
92
|
-
|
93
|
-
def read_int8
|
94
|
-
ru(1, 'c')
|
95
|
-
end
|
96
|
-
|
97
|
-
# == 16 bit
|
98
|
-
|
99
|
-
# === Unsigned
|
100
|
-
|
101
|
-
def read_word16_native
|
102
|
-
ru(2, 'S')
|
103
|
-
end
|
104
|
-
|
105
|
-
def read_word16_little
|
106
|
-
ru(2, 'v')
|
107
|
-
end
|
108
|
-
|
109
|
-
def read_word16_big
|
110
|
-
ru(2, 'n')
|
111
|
-
end
|
112
|
-
|
113
|
-
# === Signed
|
114
|
-
|
115
|
-
def read_int16_native
|
116
|
-
ru(2, 's')
|
117
|
-
end
|
118
|
-
|
119
|
-
def read_int16_little
|
120
|
-
# swap bytes if native=big (but we want little)
|
121
|
-
ru_swap(2, 's', ByteOrder::Big)
|
122
|
-
end
|
123
|
-
|
124
|
-
def read_int16_big
|
125
|
-
# swap bytes if native=little (but we want big)
|
126
|
-
ru_swap(2, 's', ByteOrder::Little)
|
127
|
-
end
|
128
|
-
|
129
|
-
# == 32 bit
|
130
|
-
|
131
|
-
# === Unsigned
|
132
|
-
|
133
|
-
def read_word32_native
|
134
|
-
ru(4, 'L')
|
135
|
-
end
|
136
|
-
|
137
|
-
def read_word32_little
|
138
|
-
ru(4, 'V')
|
139
|
-
end
|
140
|
-
|
141
|
-
def read_word32_big
|
142
|
-
ru(4, 'N')
|
143
|
-
end
|
144
|
-
|
145
|
-
# === Signed
|
146
|
-
|
147
|
-
def read_int32_native
|
148
|
-
ru(4, 'l')
|
149
|
-
end
|
150
|
-
|
151
|
-
def read_int32_little
|
152
|
-
# swap bytes if native=big (but we want little)
|
153
|
-
ru_swap(4, 'l', ByteOrder::Big)
|
154
|
-
end
|
155
|
-
|
156
|
-
def read_int32_big
|
157
|
-
# swap bytes if native=little (but we want big)
|
158
|
-
ru_swap(4, 'l', ByteOrder::Little)
|
159
|
-
end
|
160
|
-
|
161
|
-
# == Aliases
|
162
|
-
|
163
|
-
alias read_uint8 read_word8
|
164
|
-
|
165
|
-
# add some short-cut functions
|
166
|
-
%w(word16 int16 word32 int32).each do |typ|
|
167
|
-
eval %{
|
168
|
-
alias read_#{typ}_network read_#{typ}_big
|
169
|
-
def read_#{typ}(byte_order = nil)
|
170
|
-
case byte_order || @byte_order
|
171
|
-
when ByteOrder::Native then read_#{typ}_native
|
172
|
-
when ByteOrder::Little then read_#{typ}_little
|
173
|
-
when ByteOrder::Network then read_#{typ}_network
|
174
|
-
else raise ArgumentError
|
175
|
-
end
|
176
|
-
end
|
177
|
-
}
|
178
|
-
end
|
179
|
-
|
180
|
-
{:word16 => :uint16, :word32 => :uint32}.each do |old, new|
|
181
|
-
['', '_native', '_little', '_big', '_network'].each do |bo|
|
182
|
-
eval %{
|
183
|
-
alias read_#{new}#{bo} read_#{old}#{bo}
|
184
|
-
}
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
def read_cstring
|
189
|
-
str = ""
|
190
|
-
while (c=readn(1)) != "\0"
|
191
|
-
str << c
|
192
|
-
end
|
193
|
-
str
|
194
|
-
end
|
195
|
-
|
196
|
-
# read exactly n characters, otherwise raise an exception.
|
197
|
-
def readn(n)
|
198
|
-
str = read(n)
|
199
|
-
raise "couldn't read #{n} characters" if str.nil? or str.size != n
|
200
|
-
str
|
201
|
-
end
|
202
|
-
|
203
|
-
private
|
204
|
-
|
205
|
-
# shortcut method for readn+unpack
|
206
|
-
def ru(size, template)
|
207
|
-
readn(size).unpack(template).first
|
208
|
-
end
|
209
|
-
|
210
|
-
# same as method +ru+, but swap bytes if native byteorder == _byteorder_
|
211
|
-
def ru_swap(size, template, byteorder)
|
212
|
-
str = readn(size)
|
213
|
-
str.reverse! if ByteOrder.byteorder == byteorder
|
214
|
-
str.unpack(template).first
|
215
|
-
end
|
216
|
-
|
217
|
-
end
|
218
|
-
|
219
|
-
# Compatability with old version.
|
220
|
-
BinaryReader = BinReadable
|
221
|
-
|