我正在尝试为以下定义执行 ASN.1 编码/解码:
ACEI ::= SEQUENCE {
message MessageFields,
neRegNumber OCTET STRING OPTIONAL,
gpsInfo GpsInfo OPTIONAL,
siteInfo OCTET STRING OPTIONAL,
nlementID INTEGER(0..16777216) OPTIONAL,
...
}
GpsInfo ::= SEQUENCE {
gpsLat INTEGER(-900000000..900000000) OPTIONAL,
gpsLong INTEGER(-1800000000..1800000000) OPTIONAL,
gpsAlt INTEGER OPTIONAL,
...
}
MessageFields ::= SEQUENCE {
messageSequence INTEGER (1..65535),
bsId INTEGER (1..65535) OPTIONAL,
neID INTEGER(0..16777216) OPTIONAL, -- unsigned int
nelementID INTEGER(0..16777216) OPTIONAL, -- unsigned int
...
}
对应的go结构体是:
type ACEI struct {
Message MessageFields
NeRegNumber []byte `asn1:"optional"`
GPSInfo GPSInfo `asn1:"optional"`
SiteInfo []byte `asn1:"optional"`
NElementID int `asn1:"optional"`
}
type GPSInfo struct {
GpsLatitude int `asn1:"optional"`
GpsLongitude int `asn1:"optional"`
GpsAltitude int `asn1:"optional"`
}
type MessageFields struct {
MessageSequence int
BsId int `asn1:"optional"`
NeID int `asn1:"optional"`
NElementID int `asn1:"optional"`
}
我正在填充结构,编码它们,然后将它们转换为十六进制。 当我这样做时,获得的十六进制序列 (seqA) 是:
302e300f020101020204d2020215b302021a0a040430413042300b02019c020200be020200c80404304330440202309c
当我在 http://asn1-playground.oss.com/ 上做同样的事情时,我得到以下十六进制序列 (seqB):
302AA00F 80010181 0204D282 0215B383 021A0A81 020A0BA2 0B80019C 810200BE 820200C8 83020C0D 8402309C
我将这两个十六进制序列都输入到 unmarshal 函数中;虽然 seqA 已正确解码,但解码 seqB 会出现以下错误:
Error while unmarshalling: asn1: structure error: tags don't match (16 vs {class:2 tag:0 length:15 isCompound:true}) {optional:false explicit:false application:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} MessageFields @2
这是编码/解码的代码:
func main() {
//Marshalling
messageSequence := structs.MessageFields{1, 1234, 5555, 6666}
gpsInfo := structs.GPSInfo{-100, 190, 200}
val := structs.ACEI{messageSequence, []byte("0A0B"), gpsInfo, []byte("0C0D"), 12444}
hexmdata := asn1Marshal(val)
//Unmarshalling hex sequence (seqA) generated by go code
res1, _ := asn1Unmarshal(hexmdata)
fmt.Println(res1)
//Unmarshalling hex sequence (seqB) generated by http://asn1-playground.oss.com/
res2, _ := asn1Unmarshal(strings.ToLower("302AA00F800101810204D2820215B383021A0A81020A0BA20B80019C810200BE820200C883020C0D8402309C"))
fmt.Println(res2)
}
func asn1Unmarshal(hexmdata string) (structs.ACEI, error){
fmt.Println(hexmdata)
s, _ := hex.DecodeString(hexmdata)
res := structs.ACEI{}
_, err := asn1.Unmarshal(s, &res)
if err != nil {
fmt.Println("Error while unmarshalling: ", err)
}
return res, err
}
func asn1Marshal(data structs.ACEI) string {
mdata, _ := asn1.Marshal(data)
hexmdata := hex.EncodeToString(mdata)
return hexmdata
}
编辑:相反,将 go 代码序列 (seqA) 馈送到 http://asn1-playground.oss.com/ 时给我这个错误:
ACEI SEQUENCE: tag = [UNIVERSAL 16] constructed; length = 46
D0033E: Tag mismatch or tag not expected: [UNIVERSAL 16] (expected tag [0]); check field 'message' (type: MessageFields) of PDU #1 'ACEI'.
*SKIPPED*: tag = [UNIVERSAL 16] constructed; length = 15
<skipped>
D0033E: Tag mismatch or tag not expected: [UNIVERSAL 4] (expected tag [0]); check field 'message' (type: MessageFields) of PDU #1 'ACEI'.
*SKIPPED*: tag = [UNIVERSAL 4] primitive; length = 4
<skipped>
D0033E: Tag mismatch or tag not expected: [UNIVERSAL 16] (expected tag [0]); check field 'message' (type: MessageFields) of PDU #1 'ACEI'.
*SKIPPED*: tag = [UNIVERSAL 16] constructed; length = 11
<skipped>
D0033E: Tag mismatch or tag not expected: [UNIVERSAL 4] (expected tag [0]); check field 'message' (type: MessageFields) of PDU #1 'ACEI'.
*SKIPPED*: tag = [UNIVERSAL 4] primitive; length = 4
<skipped>
D0033E: Tag mismatch or tag not expected: [UNIVERSAL 2] (expected tag [0]); check field 'message' (type: MessageFields) of PDU #1 'ACEI'.
*SKIPPED*: tag = [UNIVERSAL 2] primitive; length = 2
<skipped>
D0049E: Field omitted: "message"; check PDU #1 'ACEI'.
S0012E: Decoding of PDU #1 failed with the return code '5'.
编辑 2:根据@YaFred 的建议编辑结构后,我的结构现在看起来像这样:
type ACEI struct {
Message MessageFields `asn1:"application,tag:0,implicit"`
NeRegNumber []byte `asn1:"application,tag:1,implicit,optional"`
GPSInfo GPSInfo `asn1:"application,tag:2,implicit,optional"`
SiteInfo []byte `asn1:"application,tag:3,implicit,optional"`
NElementID int `asn1:"application,tag:4,implicit,optional"`
}
type GPSInfo struct {
GpsLatitude int `asn1:"application,tag:0,implicit,optional"`
GpsLongitude int `asn1:"application,tag:1,implicit,optional"`
GpsAltitude int `asn1:"application,tag:2,implicit,optional"`
}
type MessageFields struct {
MessageSequence int `asn1:"application,tag:0,implicit"`
BsId int `asn1:"application,tag:1,implicit,optional"`
NeID int `asn1:"application,tag:2,implicit,optional"`
NElementID int `asn1:"application,tag:3,implicit,optional"`
}
使用这些结构编码给我的十六进制代码与从 asn Playground 获得的代码相同。但是,解码失败并出现以下错误:
Hex code: 302aa00f800101810204d2820215b383021a0a81020a0ba20b80019c810200be820200c883020c0d8402309c
错误(与我之前尝试使用 go 代码解码十六进制代码(来自 asn playground)时遇到的错误相同):
Error while unmarshalling: asn1: structure error: tags don't match (0 vs {class:2 tag:0 length:15 isCompound:true}) {optional:false explicit:false application:true defaultValue: tag:0xc042008348 stringType:0 timeType:0 set:false omitEmpty:false} MessageFields @2
编辑 3:从结构中删除“应用程序”标签有助于我按预期解码十六进制代码。
最佳答案
在您输入类型的地方(使用您的工具以及 http://asn1-playground.oss.com/ ),您必须使用模块。如果不是,则它不是有效的 asn1 规范(并且您的工具和 oss.com 应该拒绝它)。
My-module DEFINITIONS ::=
BEGIN
ACEI ::= SEQUENCE {
etc ...
END
工具接受在不知道模块标记上下文的情况下对类型进行编码这一事实是关于 https://stackoverflow.com/questions/tagged/asn.1 的许多问题的原因。
要回答第二个问题(关于长度的差异),你应该显示你给 oss.com 的值..它可能是:
neRegNumber '0A0B'H
siteInfo '0C0D'H
它们都是2字节长
但是当您编写 []byte("0A0B") 时,您肯定要求 Go 为您提供字符串“0A0B”的字节(实际上是 ASCII 中的 30 41 30 42)
我认为 []byte{10,11} 应该为您提供 '0A0B'H
已编辑
如果您的工具不采用 asn.1 模块,您仍然可以自己进行自动标记
ACEI ::= SEQUENCE {
message [0] IMPLICIT MessageFields,
neRegNumber [1] IMPLICIT OCTET STRING OPTIONAL,
gpsInfo [2] IMPLICIT GpsInfo OPTIONAL,
siteInfo [3] IMPLICIT OCTET STRING OPTIONAL,
nlementID [4] IMPLICIT INTEGER(0..16777216) OPTIONAL,
...
}
GpsInfo ::= SEQUENCE {
gpsLat [0] IMPLICIT INTEGER(-900000000..900000000) OPTIONAL,
gpsLong [1] IMPLICIT INTEGER(-1800000000..1800000000) OPTIONAL,
gpsAlt [2] IMPLICIT INTEGER OPTIONAL,
...
}
MessageFields ::= SEQUENCE {
messageSequence [0] IMPLICIT INTEGER (1..65535),
bsId [1] IMPLICIT INTEGER (1..65535) OPTIONAL,
neID [2] IMPLICIT INTEGER(0..16777216) OPTIONAL, -- unsigned int
nelementID [3] IMPLICIT INTEGER(0..16777216) OPTIONAL, -- unsigned int
...
}
编辑 2(基于与@Aarvi 的聊天)
我刚刚想到https://golang.org/pkg/encoding/asn1/不涉及 ASN.1 编译器。
相反,Go 结构是手动编写和注释的。
所以遵循 ASN.1 类型(在选择了 AUTOMATICS TAGS 的模块中)
GpsInfo ::= SEQUENCE {
gpsLat INTEGER(-900000000..900000000) OPTIONAL,
gpsLong INTEGER(-1800000000..1800000000) OPTIONAL,
gpsAlt INTEGER OPTIONAL,
...
}
必须这样手动写
type GPSInfo struct {
GpsLatitude int `asn1:"tag:0,implicit,optional"`
GpsLongitude int `asn1:"tag:1,implicit,optional"`
GpsAltitude int `asn1:"tag:2,implicit,optional"`
}
关于go - ASN.1 Unmarshalling using go structs 给出标签不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665475/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
如何匹配未被反斜杠转义的平衡定界符对(其本身未被反斜杠转义)(无需考虑嵌套)?例如对于反引号,我试过了,但是转义的反引号没有像转义那样工作。regex=/(?!$1:"how\\"#expected"how\\`are"上面的正则表达式不考虑由反斜杠转义并位于反引号前面的反斜杠,但我愿意考虑。StackOverflow如何做到这一点?这样做的目的并不复杂。我有文档文本,其中包括内联代码的反引号,就像StackOverflow一样,我想在HTML文件中显示它,内联代码用一些spanMaterial装饰。不会有嵌套,但转义反引号或转义反斜杠可能出现在任何地方。
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie