certificate

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

电子证照基础元数据

pkg/certificate 提供本地解析与格式检查,不加载文件、不改写原文。 OFD 的标准 XML 声明仍在 pkg/spec;业务校验不放入声明层。

metadata := certificate.ParseMetadata(docInfo.CustomDatas.CustomData)
names := metadata.Values("certificateName") // 也识别短名 ZZMC
issues := metadata.Validate()

id, err := certificate.ParseIdentifier(identifier)

可运行代码见 example_test.go。

检查范围

  • GB/T 36903—2018 §5.2 基础元数据,GB/T 36902—2018 §5.2.2、§6.3 类型代码, GB/T 36905—2018 §4 c)、§5 的标识与元数据要求。
  • 必选项:名称、类型代码、编号、标识、颁发机构名称、颁发日期、持证主体名称。 不要求目录规范中的其他必选字段;可选项不强制出现,但出现时不能留空。
  • 精确识别标准英文名或短名,保留顺序、未知字段和重复项,重复声明(含别名)报错。 certificateIdentifier 使用标准英文名,不将附录示例的 Certificate_Identifier 自动作为别名。certificateHolderCategory 与 certificateHolderTypeName 不混用。
  • ^ 多值的空项及名称、代码、代码类型的项数对应关系; 主体代码类型为 001/111 或中文名称为“统一社会信用代码”/“公民身份号码”时 要求主体代码。列表顺序的业务真实性需外部核实。
  • 完整日历日期支持 YYYY-MM-DD、YYYYMMDD、2018年7月25日; 截止日期允许“长期”,可选截止日期省略时不补写。可解释的有效期检查先后关系, 不按当前时间判定证照失效。其他日期表达仅提示 date_unchecked,不宣称其违反 GB/T 7408;不自动修剪日期或补全缺失日。
  • KZ_ 扩展原样保留,裸前缀报错;没有行业规范时不擅自判定其他未知字段无效, 不验证扩展的定义、类型、代码表和语义。

Metadata.Entries() 返回副本;Values() 返回全部声明,不以最后值覆盖前值。 Issue.Index 指向输入条目,缺失项为 -1;代码供程序分支,消息不包含原始主体代码。

标识与标准示例差异

按 GB/T 36904—2018 §5 检查: 1.2.156.3005.2.<21位类型代码>.<18位颁发机构代码>.<流水号>.<三位版本号>.<校验位>。 流水号只接受数字和英文大写字母。类型的18位机构前缀属于定义机构, 不要求它等于颁发机构。机构代码仅检查长度与字符集,不校验 GB 32100 校验位或登记。

校验位按 §5.7 的 ISO/IEC 7064 MOD 37,36 计算,排除根代码及分隔符, 字符值为 0—9 = 0—9、A—Z = 10—35:

  • GB/T 36905 附录A出现的根代码 1.2.156.3003.2 不符合 GB/T 36904 §5.2。
  • GB/T 36904 附录A(PDF 第7—8页)称计算值22对应 L,表中还有字符值与中间和不符; 按正文算法复算值22对应 M。该示例以 L 结尾时报告校验位错误。 独立 MOD 37,36 向量及该差异均有回归测试,不为示例另设兼容字母表。

解析失败可用 errors.Is(err, certificate.ErrIdentifierFormat) 或 errors.Is(err, certificate.ErrIdentifierChecksum) 区分。 不会修改文件或自动修正标识,尤其不会重写已签名原件。

OFD 文件检查

ofd-validator --certificate-metadata --format json certificate.ofd

Go API:validator.WithCertificateMetadata(true)。同时接受 TypeID 标引; 单独的 WithCustomTagTypeID(true) / --custom-tag-type-id 仍只检查标引入口。 元数据只从实际 OFD.xml/DocBody/DocInfo/CustomDatas 读取,各文档体独立检查, 不从业务标引 XML、页面文字或孤立 XML 猜测补齐。

报告检查项为 certificate_metadata,问题码为 certificate.metadata.<code>, 带 XML 位置和标准条款;certificate.scope 提示当前范围。 结构模式及 --skip-xsd 也执行元数据检查,未启用时不新增该检查项。 成功只表示这些本地检查通过,不代表全面合规或内容可信。

尚未检查:标识唯一性及历史版本递增、登记与机构授权、行业业务元数据、 代码表真实性、加注件元数据条件、锁定签名及 GM 码。无需机构联网或授权证据 也能通过本地格式检查,不能将其视为已完成这些外部验证。

原件锁定、加注件制作及重新验收由独立子包 workflow 编排,不扩大上述基础检查或 CLI 开关的范围。 该工作流要求实际锁定签名、完整信任/吊销证据和外部机构角色授权回调, 不能只凭本包的格式检查结果执行可信验收。

Documentation

Overview

Package certificate 解析、检查电子证照元数据及标识的本地格式。 本包不验证登记、唯一性、机构授权、签名或证照内容真实性。

Index

Examples

Constants

View Source
const IdentifierRoot = "1.2.156.3005.2"

IdentifierRoot 是 GB/T 36904—2018 §5.2 规定的根代码。

Variables

View Source
var (
	// ErrIdentifierFormat 表示标识组成部分不符合词法结构。
	ErrIdentifierFormat = errors.New("电子证照标识格式无效")
	// ErrIdentifierChecksum 表示 MOD 37,36 校验位不匹配。
	ErrIdentifierChecksum = errors.New("电子证照标识校验位不匹配")
)

Functions

This section is empty.

Types

type Identifier

type Identifier struct {
	// TypeCode 为定义机构代码及机构内分类码。
	TypeCode string `json:"type_code"`
	// IssuingAuthorityCode 为颁发机构代码,不要求等于类型代码前缀。
	IssuingAuthorityCode string `json:"issuing_authority_code"`
	// Serial 为颁发机构分配的流水号,保留前导零。
	Serial string `json:"serial"`
	// Version 为三位数字版本号,不转换为整数。
	Version string `json:"version"`
	// CheckCharacter 为已核对的单字符校验位。
	CheckCharacter string `json:"check_character"`
}

Identifier 是 GB/T 36904—2018 §5 的标识组成部分,保留前导零。

func ParseIdentifier

func ParseIdentifier(value string) (Identifier, error)

ParseIdentifier 检查结构与 MOD 37,36 校验位;不修剪空白、不修正根代码或大小写。 类型定义机构与颁发机构可以不同;机构代码仅检查字符集和长度,不校验登记及 GB 32100 校验位。

Example
package main

import (
	"fmt"

	"github.com/zc310/ofd/pkg/certificate"
)

func main() {
	// 附录A载荷按正文算法复算的格式示例,不证明实际登记或唯一性。
	id, err := certificate.ParseIdentifier("1.2.156.3005.2.11100000000013338W002.11320482014138860P.201807250201.001.M")
	fmt.Println(id.Version, err)
}
Output:
001 <nil>

type Issue

type Issue struct {
	// Code 为稳定的问题类别,不含校验器使用的前缀。
	Code string `json:"code"`
	// Field 为标准英文字段名,未知字段保留原名。
	Field string `json:"field"`
	// Index 为原始条目下标,缺失项为 -1。
	Index int `json:"index"`
	// Message 为不含原始主体代码的问题说明。
	Message string `json:"message"`
	// Clause 为本项检查依据的标准条款。
	Clause string `json:"clause"`
	// Warning 标记未能确定判定的检查,而非已确认错误。
	Warning bool `json:"warning,omitempty"`
}

Issue 是本地格式检查结果。Index 对应输入条目,缺少条目时为 -1。 Warning 为 true 表示当前检查范围无法作出确定判定,不是已确认的格式错误。

type Metadata

type Metadata struct {
	// contains filtered or unexported fields
}

Metadata 是有序的原始自定义元数据快照,不改写值或丢弃未知项及重复项。

func ParseMetadata

func ParseMetadata(entries []spec.CustomData) Metadata

ParseMetadata 复制已解码的 OFD DocInfo/CustomDatas;解析与校验分开,便于查看无效原文。

Example
package main

import (
	"fmt"

	"github.com/zc310/ofd/pkg/certificate"
	"github.com/zc310/ofd/pkg/spec"
)

func main() {
	// 实际条目取自 OFD DocInfo.CustomDatas.CustomData。
	m := certificate.ParseMetadata([]spec.CustomData{{Name: "ZZMC", Value: "示例证照"}})
	fmt.Println(m.Values("certificateName"))
	fmt.Println(len(m.Validate()) > 0) // 其他必选项未提供,仍可读取原始值。
}
Output:
[示例证照]
true

func (Metadata) Entries

func (m Metadata) Entries() []spec.CustomData

Entries 返回原始条目的副本。

func (Metadata) Validate

func (m Metadata) Validate() []Issue

Validate 检查原件基础元数据的必选项、重复别名、标识、日期及多值对应关系。 不要求可选项全部出现,不验证行业扩展的定义或加注件业务条件。

func (Metadata) Values

func (m Metadata) Values(name string) []string

Values 按顺序返回同一字段的全部声明值,标准英文名与短名视为同一字段。 不拆分 ^ 多值,不把 certificateHolderCategory 当作 certificateHolderTypeName。

Directories

Path Synopsis
Package workflow 编排电子证照原件锁定与加注件制作。
Package workflow 编排电子证照原件锁定与加注件制作。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL