1
2
Fork
You've already forked rasn1
0
Pure ruby library to encode/decode ASN.1 DER format
  • Ruby 100%
lemontree55 c7b231cb93
All checks were successful
ci/woodpecker/push/ci/2 Pipeline was successful
ci/woodpecker/push/ci/1 Pipeline was successful
ci/woodpecker/push/ci/5 Pipeline was successful
ci/woodpecker/push/ci/3 Pipeline was successful
ci/woodpecker/push/ci/4 Pipeline was successful
Simplify RASN1.parse
2025年12月28日 16:18:29 +01:00
.woodpecker Gemfile: update rake version for ruby 4 2025年12月28日 12:25:12 +01:00
lib Simplify RASN1.parse 2025年12月28日 16:18:29 +01:00
spec Fix Any, Null and Constrained documentation 2025年11月21日 18:43:50 +01:00
.gitignore Update .gitignore 2020年12月01日 17:48:46 +01:00
.rubocop.yml Remove support for Ruby 3.0 2025年12月28日 11:01:32 +01:00
.yardopts Fix Wrapper documentation 2025年11月21日 18:43:50 +01:00
Changelog.md Bump version to 0.16.3 2025年11月21日 19:32:10 +01:00
Gemfile Gemfile: update rake version for ruby 4 2025年12月28日 12:25:12 +01:00
LICENSE Update ownership 2024年07月13日 17:13:33 +02:00
Rakefile [Model] Fix naming 2025年08月14日 12:05:06 +02:00
rasn1.gemspec Remove support for Ruby 3.0 2025年12月28日 11:01:32 +01:00
README.md [CI] Add woodpecker-ci pipeline 2025年08月13日 17:46:48 +02:00

Gem Version status-badge

Rasn1

Rasn1 is a ruby ASN.1 library to encode, parse and decode ASN.1 data in DER format.

Installation

Add this line to your application's Gemfile:

gem 'rasn1'

And then execute:

bundle install

Or install it yourself as:

gem install rasn1

Simple usage

To decode a DER/BER string without checking a model, do:

decoded_der = RASN1.parse(der_string)
decoded_ber = RASN1.parse(ber_string, ber: true)

Advanced usage

All examples below will be based on:

Record ::= SEQUENCE {
 id INTEGER,
 room [0] INTEGER OPTIONAL,
 house [1] INTEGER DEFAULT 0
}
ComplexRecord ::= SEQUENCE {
 bool BOOLEAN,
 data [0] EXPLICIT OCTET STRING,
 a_record Record
}

Create a ASN.1 model

class Record < RASN1::Model
 sequence :record,
 content: [integer(:id),
 integer(:room, implicit: 0, optional: true),
 integer(:house, implicit: 1, default: 0)]
end

More comple classes may be designed by nesting simple classes. For example:

class ComplexRecord < RASN1::Model
 sequence :cplx_record,
 content: [boolean(:bool),
 octet_string(:data, explicit: 0),
 model(:a_record, Record)]
end

Parse a DER-encoded string

record = Record.parse(der_string)
record[:id] # => RASN1::Types::Integer
record[:id].value # => Integer
record[:id].to_i # => Integer
record[:id].asn1_class # => Symbol
record[:id].optional? # => false
record[:id].default # => nil
record[:room].optional # => true
record[:house].default # => 0
record[:id].to_der # => String
cplx_record = ComplexRecord.parse(der_string)
cplx_record[:bool] # => RASN1::Types::Boolean
cplx_record[:bool].value # => TrueClass/FalseClass
cplx_record[:data].value # => String
cplx_record[:data].explicit? # => true
cplx_record[:a_record] # => Record

Generate a DER-encoded string

record = Record.new(id: 12)
record[:id].to_i # => 12
record[:room] # => nil
record[:house] # => 0
# Set one value
record[:room] = 43
record[:room] # => 43
# Set mulitple values
record.set id: 124, house: 155
record.to_der # => String

More information

see https://github.com/sdaubert/rasn1/wiki

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/sdaubert/rasn1.