sm2-crypto 0.2.1 → 0.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/Gemfile +4 -2
- data/README.md +8 -2
- data/lib/{sm2_crypto.rb → sm2-crypto.rb} +25 -7
- metadata +5 -35
- data/Gemfile.lock +0 -22
- data/sm2-crypto.gemspec +0 -32
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 67cba5e7c65f8ade66d34435da5f014512145d0310a03667a9fd7d1db871e70f
|
|
4
|
+
data.tar.gz: a60996ef1a495c1d774573ad78c0333c6a8860fa5075542502bde1f51265e5b2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a0c4755fb05added355578173cababf90d68db821c75607fb556effccfb5594ad72433c72e947c117f0a3f4349778666da6ad6e96798b8f89f02346ba7bb10e9
|
|
7
|
+
data.tar.gz: 905491384a247b2ba18e9b59528f59459496c438292ccd83c3a6c9d1cff4a99c306c8f8bd6c5476e91c137960bb1e5ccd3953c0492cb6468463f051bf8d19aba
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.3] - 2025-09-03
|
|
4
|
+
|
|
5
|
+
- Breaking change: rename `sm2_crypto` to `sm2-crypto` to fix rails autoload
|
|
6
|
+
|
|
7
|
+
## [0.2.2] - 2025-03-26
|
|
8
|
+
|
|
9
|
+
- Add ASN.1 DER format for sm2 sign and verify
|
|
10
|
+
|
|
11
|
+
## [0.2.0] - 2023-04-21
|
|
12
|
+
|
|
13
|
+
- Add sm2 sign and verify support
|
|
14
|
+
|
|
3
15
|
## [0.1.0] - 2023-03-27
|
|
4
16
|
|
|
5
17
|
- Initial release
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -33,7 +33,7 @@ $ gem install sm2-crypto
|
|
|
33
33
|
### Encrypt and Decrypt
|
|
34
34
|
|
|
35
35
|
```ruby
|
|
36
|
-
require '
|
|
36
|
+
require 'sm2-crypto'
|
|
37
37
|
|
|
38
38
|
# Generate key pair
|
|
39
39
|
keypair = OpenSSL::PKey::EC.generate("SM2")
|
|
@@ -59,11 +59,17 @@ sign = SM2Crypto.sign(private_key, message)
|
|
|
59
59
|
# verify signatrue
|
|
60
60
|
SM2Crypto.verify(public_key, message, sign)
|
|
61
61
|
|
|
62
|
-
user_id
|
|
62
|
+
# user_id should be a hex string, default: "31323334353637383132333435363738" which is equal to utf-8 string: "1234567812345678"
|
|
63
|
+
user_id = "31323334353637383132333435363738"
|
|
63
64
|
# sign with hash and user_id
|
|
64
65
|
sign = SM2Crypto.sign(private_key, message, sm3_hash: true, user_id: user_id)
|
|
65
66
|
# verify with hash and user_id
|
|
66
67
|
SM2Crypto.verify(public_key, message, sign, sm3_hash: true, user_id: user_id)
|
|
68
|
+
|
|
69
|
+
# sign with ASN.1 DER format output
|
|
70
|
+
sign = SM2Crypto.sign(private_key, message, asn1: true)
|
|
71
|
+
# verify signatrue
|
|
72
|
+
SM2Crypto.verify(public_key, message, sign, asn1: true)
|
|
67
73
|
```
|
|
68
74
|
|
|
69
75
|
### Get Public Key from Private Key
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
require "openssl"
|
|
4
4
|
|
|
5
5
|
module SM2Crypto
|
|
6
|
+
VERSION = "0.3.0"
|
|
7
|
+
|
|
6
8
|
module_function
|
|
7
9
|
|
|
8
10
|
# Key Derived Function
|
|
@@ -101,9 +103,10 @@ module SM2Crypto
|
|
|
101
103
|
# @param private_key [String] private key, format: binary string
|
|
102
104
|
# @param data [String]
|
|
103
105
|
# @param sm3_hash [Boolean], option to sign with sm3 hash, default: false
|
|
104
|
-
# @param user_id [String], format: hex string, default: "31323334353637383132333435363738"
|
|
106
|
+
# @param user_id [String], format: hex string, default: "31323334353637383132333435363738" which is equal to utf-8 str "1234567812345678"
|
|
107
|
+
# @param asn1 [Boolean], option to return asn.1 der format signature, default: false
|
|
105
108
|
# @return [String] signature, format: hex string
|
|
106
|
-
def sign(private_key, data, sm3_hash: false, user_id: "31323334353637383132333435363738")
|
|
109
|
+
def sign(private_key, data, sm3_hash: false, user_id: "31323334353637383132333435363738", asn1: false)
|
|
107
110
|
data = data.unpack1("a*") unless data.ascii_only?
|
|
108
111
|
if sm3_hash
|
|
109
112
|
public_key = get_public_key(private_key)
|
|
@@ -130,7 +133,11 @@ module SM2Crypto
|
|
|
130
133
|
s = ((one + da).mod_inverse(n) * (k - (r * da))).to_i % n.to_i
|
|
131
134
|
end
|
|
132
135
|
|
|
133
|
-
|
|
136
|
+
if asn1
|
|
137
|
+
OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::Integer.new(r), OpenSSL::ASN1::Integer.new(s)]).to_der.unpack1("H*")
|
|
138
|
+
else
|
|
139
|
+
r.to_s(16).rjust(64, "0") + s.to_s(16).rjust(64, "0")
|
|
140
|
+
end
|
|
134
141
|
end
|
|
135
142
|
|
|
136
143
|
# verify the signature with public_key
|
|
@@ -140,17 +147,28 @@ module SM2Crypto
|
|
|
140
147
|
# @param signature [String], hex string
|
|
141
148
|
# @param sm3_hash [Boolean], option to sign with sm3 hash, default: false
|
|
142
149
|
# @param user_id [String], format: hex string, default: "31323334353637383132333435363738"
|
|
150
|
+
# @param asn1 [Boolean], option to verify asn.1 der format signature, default: false
|
|
143
151
|
# @return [Boolean] verify result
|
|
144
|
-
def verify(public_key, data, signature, sm3_hash: false, user_id: "31323334353637383132333435363738")
|
|
145
|
-
|
|
152
|
+
def verify(public_key, data, signature, sm3_hash: false, user_id: "31323334353637383132333435363738", asn1: false)
|
|
153
|
+
if asn1
|
|
154
|
+
return false if signature.size < 136
|
|
155
|
+
|
|
156
|
+
# parse asn1 der format hex string signature
|
|
157
|
+
der_seq = OpenSSL::ASN1.decode([signature].pack("H*"))
|
|
158
|
+
r = der_seq.value[0].value
|
|
159
|
+
s = der_seq.value[1].value
|
|
160
|
+
else
|
|
161
|
+
return false if signature.size != 128
|
|
162
|
+
|
|
163
|
+
r = OpenSSL::BN.new(signature[0, 64], 16)
|
|
164
|
+
s = OpenSSL::BN.new(signature[64, 64], 16)
|
|
165
|
+
end
|
|
146
166
|
|
|
147
167
|
public_key = "\x04#{public_key}" if public_key.size == 64 && public_key[0] != "\x04"
|
|
148
168
|
data = data.unpack1("a*") unless data.ascii_only?
|
|
149
169
|
if sm3_hash
|
|
150
170
|
data = OpenSSL::Digest.digest("SM3", za(public_key, user_id) + data)
|
|
151
171
|
end
|
|
152
|
-
r = OpenSSL::BN.new(signature[0, 64], 16)
|
|
153
|
-
s = OpenSSL::BN.new(signature[64, 64], 16)
|
|
154
172
|
n = OpenSSL::BN.new("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16)
|
|
155
173
|
e = OpenSSL::BN.new(data, 2)
|
|
156
174
|
|
metadata
CHANGED
|
@@ -1,43 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sm2-crypto
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Seekr
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
-
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
name: minitest
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
-
requirements:
|
|
17
|
-
- - ">="
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: '5.0'
|
|
20
|
-
type: :development
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
- - ">="
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: '5.0'
|
|
27
|
-
- !ruby/object:Gem::Dependency
|
|
28
|
-
name: rake
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
30
|
-
requirements:
|
|
31
|
-
- - ">="
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '13.0'
|
|
34
|
-
type: :development
|
|
35
|
-
prerelease: false
|
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
-
requirements:
|
|
38
|
-
- - ">="
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: '13.0'
|
|
11
|
+
date: 2025-09-03 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
41
13
|
description: sm2-crypto is an implementation of the SM2 encryption and decryption
|
|
42
14
|
algorithm in pure Ruby based on the OpenSSL
|
|
43
15
|
email:
|
|
@@ -48,12 +20,10 @@ extra_rdoc_files: []
|
|
|
48
20
|
files:
|
|
49
21
|
- CHANGELOG.md
|
|
50
22
|
- Gemfile
|
|
51
|
-
- Gemfile.lock
|
|
52
23
|
- LICENSE.txt
|
|
53
24
|
- README.md
|
|
54
25
|
- Rakefile
|
|
55
|
-
- lib/
|
|
56
|
-
- sm2-crypto.gemspec
|
|
26
|
+
- lib/sm2-crypto.rb
|
|
57
27
|
homepage: https://github.com/numbcoder/sm2-crypto
|
|
58
28
|
licenses:
|
|
59
29
|
- MIT
|
|
@@ -76,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
76
46
|
- !ruby/object:Gem::Version
|
|
77
47
|
version: '0'
|
|
78
48
|
requirements: []
|
|
79
|
-
rubygems_version: 3.
|
|
49
|
+
rubygems_version: 3.5.22
|
|
80
50
|
signing_key:
|
|
81
51
|
specification_version: 4
|
|
82
52
|
summary: An SM2 cryptographic algorithm encryption and decryption library for Ruby
|
data/Gemfile.lock
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
PATH
|
|
2
|
-
remote: .
|
|
3
|
-
specs:
|
|
4
|
-
sm2-crypto (0.2.1)
|
|
5
|
-
|
|
6
|
-
GEM
|
|
7
|
-
remote: https://rubygems.org/
|
|
8
|
-
specs:
|
|
9
|
-
minitest (5.18.0)
|
|
10
|
-
rake (13.0.6)
|
|
11
|
-
|
|
12
|
-
PLATFORMS
|
|
13
|
-
arm64-darwin-22
|
|
14
|
-
x86_64-linux
|
|
15
|
-
|
|
16
|
-
DEPENDENCIES
|
|
17
|
-
minitest (~> 5.0)
|
|
18
|
-
rake (~> 13.0)
|
|
19
|
-
sm2-crypto!
|
|
20
|
-
|
|
21
|
-
BUNDLED WITH
|
|
22
|
-
2.4.6
|
data/sm2-crypto.gemspec
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
Gem::Specification.new do |spec|
|
|
4
|
-
spec.name = "sm2-crypto"
|
|
5
|
-
spec.version = "0.2.1"
|
|
6
|
-
spec.authors = ["Seekr"]
|
|
7
|
-
spec.email = ["wzhao23@gmail.com"]
|
|
8
|
-
|
|
9
|
-
spec.summary = "An SM2 cryptographic algorithm encryption and decryption library for Ruby"
|
|
10
|
-
spec.description = "sm2-crypto is an implementation of the SM2 encryption and decryption algorithm in pure Ruby based on the OpenSSL"
|
|
11
|
-
spec.homepage = "https://github.com/numbcoder/sm2-crypto"
|
|
12
|
-
spec.license = "MIT"
|
|
13
|
-
spec.required_ruby_version = ">= 2.7.0"
|
|
14
|
-
|
|
15
|
-
spec.metadata["rubygems_mfa_required"] = "true"
|
|
16
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
|
17
|
-
spec.metadata["source_code_uri"] = spec.homepage
|
|
18
|
-
|
|
19
|
-
# Specify which files should be added to the gem when it is released.
|
|
20
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
21
|
-
spec.files = Dir.chdir(__dir__) do
|
|
22
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
|
23
|
-
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
spec.bindir = "exe"
|
|
27
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
28
|
-
spec.require_paths = ["lib"]
|
|
29
|
-
|
|
30
|
-
spec.add_development_dependency "minitest", ">= 5.0"
|
|
31
|
-
spec.add_development_dependency "rake", ">= 13.0"
|
|
32
|
-
end
|