Skip to content
Closed
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
- { os: ubuntu-20.04, ruby: head, ignore-pkg-error: true }
- { os: windows-latest, ruby: mingw, ignore-pkg-error: true }
- { os: windows-latest, ruby: mswin, ignore-pkg-error: true }
- { os: ubuntu-20.04, ruby: jruby-head, ignore-pkg-error: true }
exclude:
- { os: windows-latest, ruby: debug }

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/pkg/
/spec/reports/
/tmp/
/lib/
*.bundle
*.so
*.o
70 changes: 64 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,81 @@
require "bundler/gem_tasks"
require "rake/testtask"
require 'fileutils'

helper = Bundler::GemHelper.instance

lib_dir = RUBY_ENGINE == 'jruby' ? "lib/java" : "lib"

Rake::TestTask.new(:test) do |t|
t.libs << "test" << "test/lib" << "lib"
t.libs << "test" << "test/lib" << lib_dir
t.ruby_opts << "-rhelper"
t.test_files = FileList["test/**/test_*.rb"]
end

require 'rake/extensiontask'
Rake::ExtensionTask.new("digest")
%w(bubblebabble md5 rmd160 sha1 sha2).each do |ext|
Rake::ExtensionTask.new("digest/#{ext}")
algorithms = %w(BubbleBabble MD5 RMD160 SHA1 SHA2)

require 'rake/javaextensiontask'
Rake::JavaExtensionTask.new("digest") do |ext|
ext.source_version = '1.8'
ext.target_version = '1.8'
ext.ext_dir = 'ext/java'
ext.lib_dir = 'lib/java'
end

java_pkg = nil
task 'compile:java' => 'java:lib' do
java_pkg = Bundler::GemHelper.instance.build_java_gem
end

task 'java:lib' do
FileUtils.mkdir_p "./lib/java/digest"
maps = algorithms.each.with_object({}) {|ext, map| map[ext.downcase] = ext}
maps.each do |lib, ext|
begin
source = File.read("#{__dir__}/ext/digest/#{lib}/lib/#{lib}.rb")
source["require 'digest/#{lib}.so'"] = "JRuby::Util.load_ext('org.jruby.ext.digest.#{ext}')"
rescue
source = <<-FILE
# frozen_string_literal: true
JRuby::Util.load_ext("org.jruby.ext.digest.#{ext}")
FILE
end
File.write "./lib/java/digest/#{lib}.rb", source
end
source = File.read("#{__dir__}/ext/digest/lib/digest.rb")
source.gsub!(%r[require 'digest(?:/(\w+))?.so']) {
"JRuby::Util.load_ext('org.jruby.ext.digest.#{maps.fetch($1, 'DigestLibrary')}')"
}
source.gsub!(%r['digest/\w+\K.so(?=')], '')
File.write("./lib/java/digest.rb", source)
end

unless RUBY_ENGINE == 'jruby'
require 'rake/extensiontask'
Rake::ExtensionTask.new("digest")
algorithms.each do |ext|
Rake::ExtensionTask.new("digest/#{ext.downcase}")
end
end

task :sync_tool do
require 'fileutils'
FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
end

def helper.build_java_gem
file_name = nil
sh([*gem_command, "build", "-V", "--platform=java", spec_path]) do
file_name = built_gem_path
pkg = File.join(base, "pkg")
FileUtils.mkdir_p(pkg)
FileUtils.mv(file_name, pkg)
file_name = File.basename(file_name)
Bundler.ui.confirm "#{name} #{version} built to pkg/#{file_name}."
file_name = File.join(pkg, file_name)
end
file_name
end

task :default => :test
35 changes: 26 additions & 9 deletions digest.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,31 @@ Gem::Specification.new do |spec|

spec.bindir = "exe"
spec.executables = []
spec.require_paths = ["lib"]
spec.extensions = %w[
ext/digest/extconf.rb
ext/digest/bubblebabble/extconf.rb
ext/digest/md5/extconf.rb
ext/digest/rmd160/extconf.rb
ext/digest/sha1/extconf.rb
ext/digest/sha2/extconf.rb
]

if Gem::Platform === spec.platform and spec.platform =~ 'java' or RUBY_ENGINE == 'jruby'
spec.platform = 'java'
spec.require_paths = ["lib/java"]
spec.files.reject! {|path| path.start_with?("ext/")}
spec.files.concat [
"lib/java/digest.jar",
"lib/java/digest.rb",
"lib/java/digest/md5.rb",
"lib/java/digest/sha1.rb",
"lib/java/digest/sha2.rb",
"lib/java/digest/rmd160.rb",
"lib/java/digest/bubblebabble.rb"
]
else
spec.require_paths = ["lib"]
spec.extensions = %w[
ext/digest/extconf.rb
ext/digest/bubblebabble/extconf.rb
ext/digest/md5/extconf.rb
ext/digest/rmd160/extconf.rb
ext/digest/sha1/extconf.rb
ext/digest/sha2/extconf.rb
]
end

spec.metadata["msys2_mingw_dependencies"] = "openssl"
end
2 changes: 1 addition & 1 deletion ext/digest/lib/digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Digest
def self.const_missing(name) # :nodoc:
case name
when :SHA256, :SHA384, :SHA512
lib = 'digest/sha2.so'
lib = 'digest/sha2'
else
lib = File.join('digest', name.to_s.downcase)
end
Expand Down
119 changes: 119 additions & 0 deletions ext/java/org/jruby/ext/digest/BubbleBabble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
**** BEGIN LICENSE BLOCK *****
* BSD 2-Clause License
*
* Copyright (c) 2010, Charles Oliver Nutter <headius@headius.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***** END LICENSE BLOCK *****/

package org.jruby.ext.digest;

import org.jruby.Ruby;
import org.jruby.runtime.load.Library;
import org.jruby.util.ByteList;

import java.io.IOException;

public class BubbleBabble implements Library {

public void load(final Ruby runtime, boolean wrap) throws IOException {
RubyDigest.createDigestBubbleBabble(runtime);
}

/**
* Ported from OpenSSH (https://github.com/openssh/openssh-portable/blob/957fbceb0f3166e41b76fdb54075ab3b9cc84cba/sshkey.c#L942-L987)
*
* OpenSSH License Notice
*
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
* Copyright (c) 2008 Alexander von Gernler. All rights reserved.
* Copyright (c) 2010,2011 Damien Miller. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public static ByteList bubblebabble(byte[] message, int begin, int length) {
char[] vowels = new char[]{'a', 'e', 'i', 'o', 'u', 'y'};
char[] consonants = new char[]{'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
'n', 'p', 'r', 's', 't', 'v', 'z', 'x'};

long seed = 1;

ByteList retval = new ByteList();

int rounds = (length / 2) + 1;
retval.append('x');
for (int i = 0; i < rounds; i++) {
int idx0, idx1, idx2, idx3, idx4;

if ((i + 1 < rounds) || (length % 2 != 0)) {
long b = message[begin + 2 * i] & 0xFF;
idx0 = (int) ((((b >> 6) & 3) + seed) % 6) & 0xFFFFFFFF;
idx1 = (int) (((b) >> 2) & 15) & 0xFFFFFFFF;
idx2 = (int) (((b & 3) + (seed / 6)) % 6) & 0xFFFFFFFF;
retval.append(vowels[idx0]);
retval.append(consonants[idx1]);
retval.append(vowels[idx2]);
if ((i + 1) < rounds) {
long b2 = message[begin + (2 * i) + 1] & 0xFF;
idx3 = (int) ((b2 >> 4) & 15) & 0xFFFFFFFF;
idx4 = (int) ((b2) & 15) & 0xFFFFFFFF;
retval.append(consonants[idx3]);
retval.append('-');
retval.append(consonants[idx4]);
seed = ((seed * 5) +
((b * 7) +
b2)) % 36;
}
} else {
idx0 = (int) (seed % 6) & 0xFFFFFFFF;
idx1 = 16;
idx2 = (int) (seed / 6) & 0xFFFFFFFF;
retval.append(vowels[idx0]);
retval.append(consonants[idx1]);
retval.append(vowels[idx2]);
}
}
retval.append('x');

return retval;
}
}
44 changes: 44 additions & 0 deletions ext/java/org/jruby/ext/digest/DigestLibrary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
**** BEGIN LICENSE BLOCK *****
* BSD 2-Clause License
*
* Copyright (C) 2006 Ola Bini <ola@ologix.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***** END LICENSE BLOCK *****/

package org.jruby.ext.digest;

import java.io.IOException;

import org.jruby.Ruby;
import org.jruby.runtime.load.Library;

/**
* @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
*/
public class DigestLibrary implements Library {
public void load(final Ruby runtime, boolean wrap) throws IOException {
org.jruby.ext.digest.RubyDigest.createDigest(runtime);
}
}// DigestLibrary
41 changes: 41 additions & 0 deletions ext/java/org/jruby/ext/digest/MD5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
**** BEGIN LICENSE BLOCK *****
* BSD 2-Clause License
*
* Copyright (c) 2010, Charles Oliver Nutter <headius@headius.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***** END LICENSE BLOCK *****/

package org.jruby.ext.digest;

import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.runtime.load.Library;

public class MD5 implements Library {

public void load(final Ruby runtime, boolean wrap) throws IOException {
org.jruby.ext.digest.RubyDigest.createDigestMD5(runtime);
}
}
Loading