From 116b0f9c21f0794cfc66187944d33c5e5cc12c01 Mon Sep 17 00:00:00 2001 From: "M. Iqbal Effendi" Date: Mon, 3 Oct 2022 09:53:30 +0800 Subject: [PATCH 1/5] Feat-Ruby: add crc32 function --- ruby/lib/pehape/string/crc32.rb | 9 +++++++++ ruby/lib/pehape/string/index.rb | 1 + ruby/spec/pehape/string/crc32_spec.rb | 9 +++++++++ 3 files changed, 19 insertions(+) create mode 100644 ruby/lib/pehape/string/crc32.rb create mode 100644 ruby/spec/pehape/string/crc32_spec.rb diff --git a/ruby/lib/pehape/string/crc32.rb b/ruby/lib/pehape/string/crc32.rb new file mode 100644 index 0000000..adb4fbb --- /dev/null +++ b/ruby/lib/pehape/string/crc32.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require "zlib" + +module Pehape + def self.crc32(string) + Zlib.crc32(string.to_s) + end +end diff --git a/ruby/lib/pehape/string/index.rb b/ruby/lib/pehape/string/index.rb index b9f09a9..e39efbd 100644 --- a/ruby/lib/pehape/string/index.rb +++ b/ruby/lib/pehape/string/index.rb @@ -7,3 +7,4 @@ require_relative "strrev" require_relative "lcfirst" require_relative "ucfirst" +require_relative "crc32" diff --git a/ruby/spec/pehape/string/crc32_spec.rb b/ruby/spec/pehape/string/crc32_spec.rb new file mode 100644 index 0000000..bd952bb --- /dev/null +++ b/ruby/spec/pehape/string/crc32_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +RSpec.describe Pehape do + describe "crc32" do + it "returns the crc32 checksum of string as an integer." do + expect(Pehape.crc32("Hello World!")).to eq(472_456_355) + end + end +end From 894425af3a5b676250203f3125f1efd7bd997cd5 Mon Sep 17 00:00:00 2001 From: "M. Iqbal Effendi" Date: Mon, 3 Oct 2022 13:08:16 +0800 Subject: [PATCH 2/5] Feat-Ruby: add sha1 function --- ruby/lib/pehape/string/index.rb | 1 + ruby/lib/pehape/string/sha1.rb | 10 ++++++++++ ruby/spec/pehape/string/sha1_spec.rb | 9 +++++++++ 3 files changed, 20 insertions(+) create mode 100644 ruby/lib/pehape/string/sha1.rb create mode 100644 ruby/spec/pehape/string/sha1_spec.rb diff --git a/ruby/lib/pehape/string/index.rb b/ruby/lib/pehape/string/index.rb index e39efbd..85a67d1 100644 --- a/ruby/lib/pehape/string/index.rb +++ b/ruby/lib/pehape/string/index.rb @@ -8,3 +8,4 @@ require_relative "lcfirst" require_relative "ucfirst" require_relative "crc32" +require_relative "sha1" diff --git a/ruby/lib/pehape/string/sha1.rb b/ruby/lib/pehape/string/sha1.rb new file mode 100644 index 0000000..6447104 --- /dev/null +++ b/ruby/lib/pehape/string/sha1.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require "digest" + +module Pehape + def self.sha1(string, binary=nil) + result = Digest::SHA1.new << string.to_s + binary ? result.digest : result.hexdigest + end +end diff --git a/ruby/spec/pehape/string/sha1_spec.rb b/ruby/spec/pehape/string/sha1_spec.rb new file mode 100644 index 0000000..4eebb65 --- /dev/null +++ b/ruby/spec/pehape/string/sha1_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +RSpec.describe Pehape do + describe "sha1" do + it "returns the sha1 hash as a string." do + expect(Pehape.sha1("Hello World!")).to eq("2ef7bde608ce5404e97d5f042f95f89f1c232871") + end + end +end From 503276534f5b12507cdbb13e58b8ccfecf7ebfdd Mon Sep 17 00:00:00 2001 From: "M. Iqbal Effendi" Date: Mon, 3 Oct 2022 13:24:29 +0800 Subject: [PATCH 3/5] Feat-Ruby: add sha1_file function --- ruby/lib/pehape/string/index.rb | 1 + ruby/lib/pehape/string/sha1_file.rb | 12 ++++++++++++ ruby/spec/pehape/string/sha1_file_spec.rb | 15 +++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 ruby/lib/pehape/string/sha1_file.rb create mode 100644 ruby/spec/pehape/string/sha1_file_spec.rb diff --git a/ruby/lib/pehape/string/index.rb b/ruby/lib/pehape/string/index.rb index 85a67d1..b831d8f 100644 --- a/ruby/lib/pehape/string/index.rb +++ b/ruby/lib/pehape/string/index.rb @@ -9,3 +9,4 @@ require_relative "ucfirst" require_relative "crc32" require_relative "sha1" +require_relative "sha1_file" diff --git a/ruby/lib/pehape/string/sha1_file.rb b/ruby/lib/pehape/string/sha1_file.rb new file mode 100644 index 0000000..44ac98d --- /dev/null +++ b/ruby/lib/pehape/string/sha1_file.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "digest" + +module Pehape + def self.sha1_file(filename, binary=nil) + return false unless File.exist?(filename.to_s) + + result = Digest::SHA1.file(filename.to_s) + binary ? result.digest : result.hexdigest + end +end diff --git a/ruby/spec/pehape/string/sha1_file_spec.rb b/ruby/spec/pehape/string/sha1_file_spec.rb new file mode 100644 index 0000000..3cbc039 --- /dev/null +++ b/ruby/spec/pehape/string/sha1_file_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +RSpec.describe Pehape do + describe "sha1_file" do + it "returns file hash as a string on success." do + filename = File.expand_path("./README.md") + expect(Pehape.sha1_file(filename)).to eq("64285a71bb456bd05a5466ff7076bf2c377ecd57") + end + + it "returns false on fail." do + filename = File.expand_path("./robot.txt") + expect(Pehape.sha1_file(filename)).to eq(false) + end + end +end From 1a31175478b248c69dfcf6605cbf02491a7835db Mon Sep 17 00:00:00 2001 From: "M. Iqbal Effendi" Date: Mon, 3 Oct 2022 13:30:03 +0800 Subject: [PATCH 4/5] Feat-Ruby: add md5 function --- ruby/lib/pehape/string/index.rb | 1 + ruby/lib/pehape/string/md5.rb | 10 ++++++++++ ruby/spec/pehape/string/md5_spec.rb | 10 ++++++++++ 3 files changed, 21 insertions(+) create mode 100644 ruby/lib/pehape/string/md5.rb create mode 100644 ruby/spec/pehape/string/md5_spec.rb diff --git a/ruby/lib/pehape/string/index.rb b/ruby/lib/pehape/string/index.rb index b831d8f..1088d4e 100644 --- a/ruby/lib/pehape/string/index.rb +++ b/ruby/lib/pehape/string/index.rb @@ -10,3 +10,4 @@ require_relative "crc32" require_relative "sha1" require_relative "sha1_file" +require_relative "md5" diff --git a/ruby/lib/pehape/string/md5.rb b/ruby/lib/pehape/string/md5.rb new file mode 100644 index 0000000..c369067 --- /dev/null +++ b/ruby/lib/pehape/string/md5.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require "digest" + +module Pehape + def self.md5(string, binary=nil) + result = Digest::MD5.new << string.to_s + binary ? result.digest : result.hexdigest + end +end diff --git a/ruby/spec/pehape/string/md5_spec.rb b/ruby/spec/pehape/string/md5_spec.rb new file mode 100644 index 0000000..d3cd8d3 --- /dev/null +++ b/ruby/spec/pehape/string/md5_spec.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +RSpec.describe Pehape do + describe "md5" do + it "returns the hash as a 32-character hexadecimal number." do + expect(Pehape.md5("Hello World!")).to eq("ed076287532e86365e841e92bfc50d8c") + expect(Pehape.md5("apple")).to eq("1f3870be274f6c49b3e31a0c6728957f") + end + end +end From e3146244055e36a95180815f719b847386d09222 Mon Sep 17 00:00:00 2001 From: "M. Iqbal Effendi" Date: Mon, 3 Oct 2022 13:33:13 +0800 Subject: [PATCH 5/5] Feat-Ruby: add md5_file function --- ruby/lib/pehape/string/index.rb | 1 + ruby/lib/pehape/string/md5_file.rb | 12 ++++++++++++ ruby/spec/pehape/string/md5_file_spec.rb | 15 +++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 ruby/lib/pehape/string/md5_file.rb create mode 100644 ruby/spec/pehape/string/md5_file_spec.rb diff --git a/ruby/lib/pehape/string/index.rb b/ruby/lib/pehape/string/index.rb index 1088d4e..feb0359 100644 --- a/ruby/lib/pehape/string/index.rb +++ b/ruby/lib/pehape/string/index.rb @@ -11,3 +11,4 @@ require_relative "sha1" require_relative "sha1_file" require_relative "md5" +require_relative "md5_file" diff --git a/ruby/lib/pehape/string/md5_file.rb b/ruby/lib/pehape/string/md5_file.rb new file mode 100644 index 0000000..5e917dd --- /dev/null +++ b/ruby/lib/pehape/string/md5_file.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "digest" + +module Pehape + def self.md5_file(filename, binary=nil) + return false unless File.exist?(filename.to_s) + + result = Digest::MD5.file(filename.to_s) + binary ? result.digest : result.hexdigest + end +end diff --git a/ruby/spec/pehape/string/md5_file_spec.rb b/ruby/spec/pehape/string/md5_file_spec.rb new file mode 100644 index 0000000..9a4a5ef --- /dev/null +++ b/ruby/spec/pehape/string/md5_file_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +RSpec.describe Pehape do + describe "md5_file" do + it "returns a string on success." do + filename = File.expand_path("./README.md") + expect(Pehape.md5_file(filename)).to eq("6cccfa61af5a5310f08f3fabcbe230ae") + end + + it "returns false on fail." do + filename = File.expand_path("./robot.txt") + expect(Pehape.md5_file(filename)).to eq(false) + end + end +end