string_entropy 0.1.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.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
6
+ after_script: codeclimate-test-reporter
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in string_entropy.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem "simplecov"
8
+ gem "codeclimate-test-reporter", "~> 1.0.0"
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 anothermh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # string_entropy
2
+
3
+ [![Build Status](https://travis-ci.org/anothermh/string_entropy.svg?branch=master)](https://travis-ci.org/anothermh/string_entropy)
4
+ [![Code Climate](https://codeclimate.com/github/anothermh/string_entropy/badges/gpa.svg)](https://codeclimate.com/github/anothermh/string_entropy)
5
+ [![Test Coverage](https://codeclimate.com/github/anothermh/string_entropy/badges/coverage.svg)](https://codeclimate.com/github/anothermh/string_entropy/coverage)
6
+
7
+ Outputs the information entropy, Shannon entropy, and metric entropy of any given `ASCII_8BIT` string.
8
+
9
+ More information on entropy:
10
+
11
+ * https://en.wikipedia.org/wiki/Entropy_%28information_theory%29
12
+ * https://en.wikibooks.org/wiki/An_Intuitive_Guide_to_the_Concept_of_Entropy_Arising_in_Various_Sectors_of_Science
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'string_entropy'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install string_entropy
29
+
30
+ ## Usage
31
+
32
+ As an extension to the `String` class:
33
+
34
+ ```ruby
35
+ "password".entropy
36
+ => 52.55884486664758
37
+ ```
38
+
39
+ With the `StringEntropy::Info` class:
40
+
41
+ ```ruby
42
+ string_entropy = StringEntropy.info("password")
43
+ => #<StringEntropy::Info:0x007fb864091590
44
+ @count={"p"=>1.0, "a"=>1.0, "s"=>2.0, "w"=>1.0, "o"=>1.0, "r"=>1.0, "d"=>1.0},
45
+ @frequency=
46
+ {"p"=>0.125,
47
+ "a"=>0.125,
48
+ "s"=>0.25,
49
+ "w"=>0.125,
50
+ "o"=>0.125,
51
+ "r"=>0.125,
52
+ "d"=>0.125},
53
+ @information_entropy=52.55884486664758,
54
+ @metric_entropy=0.34375,
55
+ @shannon_entropy=2.75,
56
+ @shannon_formulas=
57
+ ["H(X) = -[ ((0.125 * Math.log2(0.125))+(0.125 * Math.log2(0.125))+(0.25 * Math.log2(0.25))+(0.125 * Math.log2(0.125))+(0.125 * Math.log2(0.125))+(0.125 * Math.log2(0.125))+(0.125 * Math.log2(0.125))) ]",
58
+ "H(X) = -[ ((-0.375)+(-0.375)+(-0.5)+(-0.375)+(-0.375)+(-0.375)+(-0.375)) ]",
59
+ "H(X) = -[ (-2.75) ]",
60
+ "H(X) = 2.75"],
61
+ @string="password">
62
+
63
+ # Original string
64
+ string_entropy.string
65
+ => "password"
66
+
67
+ # String encoding, currently only supports ASCII_8BIT
68
+ string_entropy.encoding
69
+ => "ASCII-8BIT"
70
+
71
+ # The commonly described "entropy" of a string; Math.log2(total_possible_chars^string_length)
72
+ string_entropy.entropy
73
+ => 52.55884486664758
74
+
75
+ # Alias for .entropy method
76
+ string_entropy.information_entropy
77
+ => 52.55884486664758
78
+
79
+ # The Shannon entropy value
80
+ string_entropy.shannon_entropy
81
+ => 2.75
82
+
83
+ # The metric entropy value (Shannon entropy divided by string length)
84
+ string_entropy.metric_entropy
85
+ => 0.34375
86
+
87
+ # The count of each character
88
+ string_entropy.count
89
+ => {
90
+ "p" => 1.0,
91
+ "a" => 1.0,
92
+ "s" => 2.0,
93
+ "w" => 1.0,
94
+ "o" => 1.0,
95
+ "r" => 1.0,
96
+ "d" => 1.0
97
+ }
98
+
99
+ # The frequency/percentage of each character
100
+ string_entropy.frequency
101
+ => {
102
+ "p" => 0.125,
103
+ "a" => 0.125,
104
+ "s" => 0.25,
105
+ "w" => 0.125,
106
+ "o" => 0.125,
107
+ "r" => 0.125,
108
+ "d" => 0.125
109
+ }
110
+
111
+ # This is how the Shannon entropy is calculated
112
+ string_entropy.shannon_formulas
113
+ => [
114
+ [0] "H(X) = -[ ((0.125 * Math.log2(0.125))+(0.125 * Math.log2(0.125))+(0.25 * Math.log2(0.25))+(0.125 * Math.log2(0.125))+(0.125 * Math.log2(0.125))+(0.125 * Math.log2(0.125))+(0.125 * Math.log2(0.125))) ]",
115
+ [1] "H(X) = -[ ((-0.375)+(-0.375)+(-0.5)+(-0.375)+(-0.375)+(-0.375)+(-0.375)) ]",
116
+ [2] "H(X) = -[ (-2.75) ]",
117
+ [3] "H(X) = 2.75"
118
+ ]
119
+ ```
120
+
121
+ ## Development
122
+
123
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
124
+
125
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
126
+
127
+ ## Credits
128
+
129
+ Inspired by http://www.shannonentropy.netmark.pl.
130
+
131
+ ## Contributing
132
+
133
+ Bug reports and pull requests are welcome on GitHub at https://github.com/anothermh/string_entropy.
134
+
135
+ ## License
136
+
137
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "string_entropy"
5
+
6
+ require "pry"
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,16 @@
1
+ require "string_entropy/error"
2
+ require "string_entropy/info"
3
+ require "string_entropy/version"
4
+ require "string_entropy/string"
5
+
6
+ module StringEntropy
7
+
8
+ def self.info(string)
9
+ StringEntropy::Info.new(string)
10
+ end
11
+
12
+ def self.example
13
+ StringEntropy::Info.new("password")
14
+ end
15
+
16
+ end
@@ -0,0 +1,23 @@
1
+ module StringEntropy
2
+
3
+ class Error < StandardError; end
4
+
5
+ class BadEncoding < Error
6
+ def initialize
7
+ super("The string could not be converted to ASCII_8BIT encoding")
8
+ end
9
+ end
10
+
11
+ class StringMismatch < Error
12
+ def initialize
13
+ super("The string contains invalid ASCII_8BIT characters (only character codes 32..126 are valid)")
14
+ end
15
+ end
16
+
17
+ class NilHasZeroEntropy < Error
18
+ def initialize
19
+ super("nil values cannot be evaluated as strings and have zero entropy")
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,64 @@
1
+ module StringEntropy
2
+ class Info
3
+
4
+ attr_reader :string,
5
+ :encoding,
6
+ :count,
7
+ :frequency,
8
+ :information_entropy,
9
+ :shannon_entropy,
10
+ :metric_entropy
11
+
12
+ # The common defintion of "entropy" when working with strings
13
+ alias_method :entropy, :information_entropy
14
+
15
+ # Supports only ASCII_8BIT encoding
16
+ POSSIBLE_CHARS = 95.freeze
17
+
18
+ def initialize(string=nil)
19
+ @string = ascii(string)
20
+ @count = @string.split("").each.with_object(Hash.new(0.to_f)) { |char, hash| hash[char] += 1 }.freeze
21
+ @frequency = @count.each_with_object(Hash.new) { |(char,count),frequency| frequency[char] = (count / @string.length) }.freeze
22
+
23
+ @information_entropy = Math.log2(POSSIBLE_CHARS**@string.length).freeze
24
+ @shannon_entropy = -(@frequency.values.map { |x| x * Math.log2(x) }.inject(:+)).freeze
25
+ @metric_entropy = (@shannon_entropy / @string.length).freeze
26
+
27
+ @shannon_formulas = shannon_formulas
28
+
29
+ self.freeze
30
+ end
31
+
32
+ # Show the math behind calculating Shannon Entropy as an array of the steps taken
33
+ def shannon_formulas
34
+ formulas = []
35
+ formulas << @frequency.values.map { |x| "(#{x} * Math.log2(#{x}))" }.join("+")
36
+ formulas << @frequency.values.map { |x| "(#{x * Math.log2(x)})" }.join("+")
37
+ formulas << "#{@frequency.values.map { |x| x * Math.log2(x) }.inject(:+)}"
38
+ formulas.map! { |x| "-[ (#{x}) ]" }
39
+ formulas << "#{@shannon_entropy}"
40
+ formulas.map! { |x| "H(X) = #{x}"}
41
+ formulas.freeze
42
+ end
43
+
44
+ private
45
+
46
+ def ascii(string)
47
+ raise NilHasZeroEntropy unless string
48
+
49
+ begin
50
+ ascii = string.encode(Encoding::ASCII_8BIT)
51
+ rescue Encoding::UndefinedConversionError
52
+ raise BadEncoding
53
+ end
54
+
55
+ safe_ascii = ascii.split("").select { |x| true if (32..126) === x.ord }.join("")
56
+ raise StringMismatch unless ascii == safe_ascii && ascii == string
57
+
58
+ @encoding = Encoding::ASCII_8BIT.name
59
+
60
+ safe_ascii.freeze
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def entropy
3
+ StringEntropy::Info.new(self).information_entropy
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module StringEntropy
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'string_entropy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "string_entropy"
8
+ spec.version = StringEntropy::VERSION
9
+ spec.authors = ["anothermh"]
10
+
11
+ spec.summary = "Gets the entropy for a given string"
12
+ spec.description = "Outputs a variety of information on the entropy of a given string"
13
+ spec.homepage = "https://github.com/anothermh/string_entropy"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.13"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "pry", "~> 0.10.4"
36
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: string_entropy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - anothermh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.4
69
+ description: Outputs a variety of information on the entropy of a given string
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".codeclimate.yml"
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/string_entropy.rb
87
+ - lib/string_entropy/error.rb
88
+ - lib/string_entropy/info.rb
89
+ - lib/string_entropy/string.rb
90
+ - lib/string_entropy/version.rb
91
+ - string_entropy.gemspec
92
+ homepage: https://github.com/anothermh/string_entropy
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Gets the entropy for a given string
116
+ test_files: []