foxify 0.9.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5e8da54e157337458828306835a65bfbcb181af66075d09932127ca411a8320a
4
+ data.tar.gz: caf2d236c36f6937402b1172ef092d4dbe18105657a4cd7a4aef770a4f2acd6e
5
+ SHA512:
6
+ metadata.gz: 87abb84bb4ed756f8c78cc12077819b1e0f680b3c30aae08e2d213675ea3a79228c96ef3d1766d91a209319f3161b833da66778b78965ec86e37ef703d44c682
7
+ data.tar.gz: '09b84594260f403bd94234c0d1422eba51dd6fd6cc427f5239a44bc9ccd40c48046cf8a3eb99b5da8d9e08b0e440e61e29f7d5f9557353966af79f427c1a1211'
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.3
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ EnforcedStyle: double_quotes
10
+
11
+ Metrics/BlockLength:
12
+ Exclude:
13
+ - "./spec/**/*"
14
+ - "foxify.gemspec"
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## [0.9.1] - 2025-01-12
2
+
3
+ - Fix gemspec allowed_push_host
4
+
5
+ ## [0.9.0] - 2025-01-12
6
+
7
+ - Initial release of Foxify::ResumableSHA256 and Foxify::ResumableSHA1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Oli Kessler
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,96 @@
1
+ [![Test](https://github.com/orlon-pro/foxify/actions/workflows/main.yml/badge.svg)](https://github.com/orlon-pro/foxify/actions/workflows/main.yml)
2
+
3
+ # Foxify
4
+
5
+ `foxify` is a gem which provides resumable digest implementations in ruby by
6
+ leveraging the power of [Go](https://go.dev/)
7
+
8
+ The classes `Foxify::ResumableSHA256` and `Foxify::ResumableSHA1` can be safely
9
+ serialized and restored at any point in time and digest calculation can be resumed.
10
+ This is an advantage in stateless application servers where i.e. large files are
11
+ uploaded in chunks and you want to resume digest calculation in each request.
12
+
13
+ ## Requirements
14
+
15
+ * Ruby 3.3+
16
+ * Go 1.23+
17
+
18
+ ## Installation
19
+
20
+ Add the foxify gem to your Gemfile:
21
+
22
+ ```text
23
+ gem "foxify"
24
+ ```
25
+
26
+ Update your bundle:
27
+
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ **NOTE**: You need a go-lang compiler version 1.23+ in your environment to compile the native part of this gem.
33
+
34
+ ## Usage
35
+
36
+ ### Foxify::ResumableSHA256
37
+
38
+ Simple usage without serializing:
39
+
40
+ ```ruby
41
+ require 'foxify'
42
+
43
+ digest = Foxify::ResumableSHA256.new
44
+ digest.update("The quick brown fox jumps over the lazy dog")
45
+ digest.hexdigest # returns "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"
46
+ ```
47
+
48
+ Usage with serializing and resuming later on:
49
+
50
+ ```ruby
51
+ require 'foxify'
52
+
53
+ digest = Foxify::ResumableSHA256.new
54
+ digest.update("The quick brown fox ")
55
+ data = digest.to_msgpack
56
+
57
+ # store data somewhere and later reload it
58
+ restored = Foxify::ResumableSHA256.from_msgpack(data)
59
+ restored.update("jumps over the lazy dog")
60
+ restored.hexdigest # returns "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"
61
+ ```
62
+
63
+ ### Foxify::ResumableSHA1
64
+
65
+ Simple usage without serializing:
66
+
67
+ ```ruby
68
+ require 'foxify'
69
+
70
+ digest = Foxify::ResumableSHA1.new
71
+ digest.update("The quick brown fox jumps over the lazy dog")
72
+ digest.hexdigest # returns "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
73
+ ```
74
+
75
+ Usage with serializing and resuming later on:
76
+
77
+ ```ruby
78
+ require 'foxify'
79
+
80
+ digest = Foxify::ResumableSHA1.new
81
+ digest.update("The quick brown fox ")
82
+ data = digest.to_msgpack
83
+
84
+ # store data somewhere and later reload it
85
+ restored = Foxify::ResumableSHA1.from_msgpack(data)
86
+ restored.update("jumps over the lazy dog")
87
+ restored.hexdigest # returns "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
88
+ ```
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/orlon-pro/foxify.
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ require "rake/extensiontask"
13
+
14
+ task build: :compile
15
+
16
+ GEMSPEC = Gem::Specification.load("foxify.gemspec")
17
+
18
+ Rake::ExtensionTask.new("foxify", GEMSPEC) do |ext|
19
+ ext.lib_dir = "lib/foxify"
20
+ end
21
+
22
+ task default: %i[clobber compile spec rubocop]
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+ require "go_gem/mkmf"
5
+
6
+ # Makes all symbols private by default to avoid unintended conflict
7
+ # with other gems. To explicitly export symbols you can use RUBY_FUNC_EXPORTED
8
+ # selectively, or entirely remove this flag.
9
+ append_cflags("-fvisibility=hidden")
10
+
11
+ create_go_makefile("foxify/foxify")
@@ -0,0 +1,2 @@
1
+ #include "foxify.h"
2
+ #include "_cgo_export.h"
@@ -0,0 +1,137 @@
1
+ package main
2
+
3
+ /*
4
+ #include "foxify.h"
5
+
6
+ VALUE rb_foxify_resumable_sha256_init(VALUE self);
7
+ VALUE rb_foxify_resumable_sha256_update(VALUE self, VALUE state, VALUE data);
8
+ VALUE rb_foxify_resumable_sha256_finalize(VALUE self, VALUE state);
9
+ VALUE rb_foxify_resumable_sha1_init(VALUE self);
10
+ VALUE rb_foxify_resumable_sha1_update(VALUE self, VALUE state, VALUE data);
11
+ VALUE rb_foxify_resumable_sha1_finalize(VALUE self, VALUE state);
12
+ */
13
+ import "C"
14
+
15
+ import (
16
+ "crypto/sha1"
17
+ "crypto/sha256"
18
+ "encoding"
19
+ "encoding/base64"
20
+ "encoding/hex"
21
+ "log"
22
+
23
+ "github.com/ruby-go-gem/go-gem-wrapper/ruby"
24
+ )
25
+
26
+ //export rb_foxify_resumable_sha256_init
27
+ func rb_foxify_resumable_sha256_init(_ C.VALUE) C.VALUE {
28
+ h := sha256.New()
29
+
30
+ marshaler := h.(encoding.BinaryMarshaler)
31
+ state, err := marshaler.MarshalBinary()
32
+ if err != nil {
33
+ log.Fatal("Unable to marshal hash:", err)
34
+ }
35
+
36
+ enc := base64.StdEncoding.EncodeToString(state)
37
+ return C.VALUE(ruby.String2Value(enc))
38
+ }
39
+
40
+ //export rb_foxify_resumable_sha1_init
41
+ func rb_foxify_resumable_sha1_init(_ C.VALUE) C.VALUE {
42
+ h := sha1.New()
43
+
44
+ marshaler := h.(encoding.BinaryMarshaler)
45
+ state, err := marshaler.MarshalBinary()
46
+ if err != nil {
47
+ log.Fatal("Unable to marshal hash:", err)
48
+ }
49
+
50
+ enc := base64.StdEncoding.EncodeToString(state)
51
+ return C.VALUE(ruby.String2Value(enc))
52
+ }
53
+
54
+ //export rb_foxify_resumable_sha256_update
55
+ func rb_foxify_resumable_sha256_update(_ C.VALUE, state C.VALUE, data C.VALUE) C.VALUE {
56
+ s, _ := base64.StdEncoding.DecodeString(ruby.Value2String(ruby.VALUE(state)))
57
+ h := sha256.New()
58
+ unmarshaler := h.(encoding.BinaryUnmarshaler)
59
+ unmarshaler.UnmarshalBinary(s)
60
+
61
+ buffer := ruby.Value2String(ruby.VALUE(data))
62
+ if len(buffer) > 0 {
63
+ h.Write([]byte(buffer))
64
+ }
65
+
66
+ marshaler := h.(encoding.BinaryMarshaler)
67
+ new_state, err := marshaler.MarshalBinary()
68
+ if err != nil {
69
+ log.Fatal("Unable to marshal hash:", err)
70
+ }
71
+
72
+ enc := base64.StdEncoding.EncodeToString(new_state)
73
+ return C.VALUE(ruby.String2Value(enc))
74
+ }
75
+
76
+ //export rb_foxify_resumable_sha1_update
77
+ func rb_foxify_resumable_sha1_update(_ C.VALUE, state C.VALUE, data C.VALUE) C.VALUE {
78
+ s, _ := base64.StdEncoding.DecodeString(ruby.Value2String(ruby.VALUE(state)))
79
+ h := sha1.New()
80
+ unmarshaler := h.(encoding.BinaryUnmarshaler)
81
+ unmarshaler.UnmarshalBinary(s)
82
+
83
+ buffer := ruby.Value2String(ruby.VALUE(data))
84
+ if len(buffer) > 0 {
85
+ h.Write([]byte(buffer))
86
+ }
87
+
88
+ marshaler := h.(encoding.BinaryMarshaler)
89
+ new_state, err := marshaler.MarshalBinary()
90
+ if err != nil {
91
+ log.Fatal("Unable to marshal hash:", err)
92
+ }
93
+
94
+ enc := base64.StdEncoding.EncodeToString(new_state)
95
+ return C.VALUE(ruby.String2Value(enc))
96
+ }
97
+
98
+ //export rb_foxify_resumable_sha256_finalize
99
+ func rb_foxify_resumable_sha256_finalize(_ C.VALUE, state C.VALUE) C.VALUE {
100
+ s, _ := base64.StdEncoding.DecodeString(ruby.Value2String(ruby.VALUE(state)))
101
+ h := sha256.New()
102
+ unmarshaler := h.(encoding.BinaryUnmarshaler)
103
+ unmarshaler.UnmarshalBinary(s)
104
+
105
+ result := h.Sum(nil)
106
+ return C.VALUE(ruby.String2Value(hex.EncodeToString(result)))
107
+ }
108
+
109
+ //export rb_foxify_resumable_sha1_finalize
110
+ func rb_foxify_resumable_sha1_finalize(_ C.VALUE, state C.VALUE) C.VALUE {
111
+ s, _ := base64.StdEncoding.DecodeString(ruby.Value2String(ruby.VALUE(state)))
112
+ h := sha1.New()
113
+ unmarshaler := h.(encoding.BinaryUnmarshaler)
114
+ unmarshaler.UnmarshalBinary(s)
115
+
116
+ result := h.Sum(nil)
117
+ return C.VALUE(ruby.String2Value(hex.EncodeToString(result)))
118
+ }
119
+
120
+ //export Init_foxify
121
+ func Init_foxify() {
122
+ rb_mFoxify := ruby.RbDefineModule("Foxify")
123
+ rb_mNative := ruby.RbDefineModuleUnder(rb_mFoxify, "Native")
124
+
125
+ // SHA256
126
+ ruby.RbDefineSingletonMethod(rb_mNative, "sha256_init", C.rb_foxify_resumable_sha256_init, 0)
127
+ ruby.RbDefineSingletonMethod(rb_mNative, "sha256_update", C.rb_foxify_resumable_sha256_update, 2)
128
+ ruby.RbDefineSingletonMethod(rb_mNative, "sha256_finalize", C.rb_foxify_resumable_sha256_finalize, 1)
129
+
130
+ // SHA1
131
+ ruby.RbDefineSingletonMethod(rb_mNative, "sha1_init", C.rb_foxify_resumable_sha1_init, 0)
132
+ ruby.RbDefineSingletonMethod(rb_mNative, "sha1_update", C.rb_foxify_resumable_sha1_update, 2)
133
+ ruby.RbDefineSingletonMethod(rb_mNative, "sha1_finalize", C.rb_foxify_resumable_sha1_finalize, 1)
134
+ }
135
+
136
+ func main() {
137
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef FOXIFY_H
2
+ #define FOXIFY_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* FOXIFY_H */
data/ext/foxify/go.mod ADDED
@@ -0,0 +1,5 @@
1
+ module github.com/username/foxify
2
+
3
+ go 1.23.4
4
+
5
+ require github.com/ruby-go-gem/go-gem-wrapper v0.6.0 // indirect
data/ext/foxify/go.sum ADDED
@@ -0,0 +1,2 @@
1
+ github.com/ruby-go-gem/go-gem-wrapper v0.6.0 h1:WFu2Cj/uzKAOemsrCo4P6vsdOgB5yesrrJtAqvLkAso=
2
+ github.com/ruby-go-gem/go-gem-wrapper v0.6.0/go.mod h1:k2k+LziSCMxNYP4J9/9v90xdU6zlU1DJpJDTU6oJhHE=
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "msgpack"
4
+
5
+ module Foxify
6
+ # A resumable SHA1 implementation
7
+ class ResumableSHA1
8
+ attr_reader :state, :finalized
9
+
10
+ def initialize(state = nil, finalized: false)
11
+ @state = state
12
+ @finalized = finalized
13
+ reset unless @state
14
+ end
15
+
16
+ def reset
17
+ @state = Foxify::Native.sha1_init
18
+ @finalized = false
19
+ self
20
+ end
21
+
22
+ def update(data)
23
+ raise Foxify::Error "Invalid state - you must reset this instance before adding new data" if @finalized
24
+
25
+ @state = Foxify::Native.sha1_update(@state, data)
26
+ self
27
+ end
28
+
29
+ def hexdigest
30
+ raise Foxify::Error, "Invalid state - this is already finalized" if @finalized
31
+
32
+ Foxify::Native.sha1_finalize(@state).tap do
33
+ @finalized = true
34
+ end
35
+ end
36
+
37
+ def ==(other)
38
+ self.class == other.class && state == other.state && finalized == other.finalized
39
+ end
40
+
41
+ def to_msgpack
42
+ [@state, @finalized].to_msgpack
43
+ end
44
+
45
+ def self.from_msgpack(data)
46
+ state, finalized = MessagePack.unpack(data)
47
+ new(state, finalized:)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "msgpack"
4
+
5
+ module Foxify
6
+ # A resumable SHA256 implementation
7
+ class ResumableSHA256
8
+ attr_reader :state, :finalized
9
+
10
+ def initialize(state = nil, finalized: false)
11
+ @state = state
12
+ @finalized = finalized
13
+ reset unless @state
14
+ end
15
+
16
+ def reset
17
+ @state = Foxify::Native.sha256_init
18
+ @finalized = false
19
+ self
20
+ end
21
+
22
+ def update(data)
23
+ raise Foxify::Error "Invalid state - you must reset this instance before adding new data" if @finalized
24
+
25
+ @state = Foxify::Native.sha256_update(@state, data)
26
+ self
27
+ end
28
+
29
+ def hexdigest
30
+ raise Foxify::Error, "Invalid state - this is already finalized" if @finalized
31
+
32
+ Foxify::Native.sha256_finalize(@state).tap do
33
+ @finalized = true
34
+ end
35
+ end
36
+
37
+ def ==(other)
38
+ self.class == other.class && state == other.state && finalized == other.finalized
39
+ end
40
+
41
+ def to_msgpack
42
+ [@state, @finalized].to_msgpack
43
+ end
44
+
45
+ def self.from_msgpack(data)
46
+ state, finalized = MessagePack.unpack(data)
47
+ new(state, finalized:)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Foxify
4
+ VERSION = "0.9.1"
5
+ end
data/lib/foxify.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "foxify/version"
4
+ require_relative "foxify/foxify"
5
+ require_relative "foxify/resumable_sha256"
6
+ require_relative "foxify/resumable_sha1"
7
+
8
+ module Foxify
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
data/sig/foxify.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Foxify
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foxify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Dr. Zarkov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-01-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: go_gem
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: msgpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Provides ruby implementation of resumable digests based on golang implementation
42
+ email:
43
+ - zarkov@tor.ch
44
+ executables: []
45
+ extensions:
46
+ - ext/foxify/extconf.rb
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - CHANGELOG.md
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - ext/foxify/extconf.rb
56
+ - ext/foxify/foxify.c
57
+ - ext/foxify/foxify.go
58
+ - ext/foxify/foxify.h
59
+ - ext/foxify/go.mod
60
+ - ext/foxify/go.sum
61
+ - lib/foxify.rb
62
+ - lib/foxify/resumable_sha1.rb
63
+ - lib/foxify/resumable_sha256.rb
64
+ - lib/foxify/version.rb
65
+ - sig/foxify.rbs
66
+ homepage: https://github.com/orlon-pro/foxify
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/orlon-pro/foxify
71
+ source_code_uri: https://github.com/orlon-pro/foxify.
72
+ changelog_uri: https://github.com/orlon-pro/foxify/CHANGELOG.md
73
+ rubygems_mfa_required: 'true'
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.3.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.5.10
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Implementation of resumable digests and other nifty foxalike things
93
+ test_files: []