Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat-Ruby: add crc32, sha1, and md5 function #54

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ruby/lib/pehape/string/crc32.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "zlib"

module Pehape
def self.crc32(string)
Zlib.crc32(string.to_s)
end
end
5 changes: 5 additions & 0 deletions ruby/lib/pehape/string/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@
require_relative "hex2bin"
require_relative "str_rot13"
require_relative "ord"
require_relative "crc32"
require_relative "sha1"
require_relative "sha1_file"
require_relative "md5"
require_relative "md5_file"
10 changes: 10 additions & 0 deletions ruby/lib/pehape/string/md5.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions ruby/lib/pehape/string/md5_file.rb
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kalo di php emang dia return false kalo file ngga exists?


result = Digest::MD5.file(filename.to_s)
binary ? result.digest : result.hexdigest
end
end
10 changes: 10 additions & 0 deletions ruby/lib/pehape/string/sha1.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions ruby/lib/pehape/string/sha1_file.rb
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kalo di php emang dia return false kalo filenya ngga exist?


result = Digest::SHA1.file(filename.to_s)
binary ? result.digest : result.hexdigest
end
end
9 changes: 9 additions & 0 deletions ruby/spec/pehape/string/crc32_spec.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions ruby/spec/pehape/string/md5_file_spec.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions ruby/spec/pehape/string/md5_spec.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions ruby/spec/pehape/string/sha1_file_spec.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions ruby/spec/pehape/string/sha1_spec.rb
Original file line number Diff line number Diff line change
@@ -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