Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

ECDSA registry signatures aren't reproducible

未关闭
#1,237 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
停滞
技术栈
go

调研方向

先从 registry signatures guide 以及链接的 registry version 和 key endpoints 开始,然后将它们与提供的 Go verification example 进行比较。记录可复现 ECDSA verification 所需的 hashing 和 encoding 细节,并向文档中添加一个可运行的示例。

由索引模型根据 Issue 内容生成。

描述

I'm following the guide at https://docs.npmjs.com/about-registry-signatures, and I'm struggling to verify the ECDSA signatures that get published to the NPM registry.

Some example code, that shows things like which hashing algorithm is used for the contents of bundles (I'm just assuming that it's sha256 currently), and whether the keys are base64 encoded using the standard algorithm or the alternate under RFC 4648, would be immensely helpful.

I've written what I have so far below, and would appreciate some help getting it to work. Feel free to add it to your documentation once it does!

package main

import (
	"bytes"
	"crypto/ecdsa"
	"crypto/sha256"
	"crypto/x509"
	"encoding/base64"
	"fmt"
	"io"
	"net/http"
)

func downloadPackage(compressedPackage *bytes.Buffer) error {
	resp, err := http.Get("https://registry.npmjs.org/light-cycle/-/light-cycle-1.4.3.tgz")
	if err != nil {
		return fmt.Errorf("package download failed: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("pacakge download bad status code: %s", resp.Status)
	}

	_, err = io.Copy(compressedPackage, resp.Body)
	if err != nil {
		return fmt.Errorf("failed to read package to buffer: %v", err)
	}

	return nil
}

func verifyPackage(compressedPackage *bytes.Buffer) error {
	// From https://registry.npmjs.org/light-cycle/1.4.3.
	versionSignature := "MEUCIQCX/49atNeSDYZP8betYWEqB0G8zZnIyB7ibC7nRNyMiQIgHosOKHhVTVNBI/6iUNSpDokOc44zsZ7TfybMKj8YdfY="

	// From https://registry.npmjs.org/-/npm/v1/keys.
	publicKey := "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1Olb3zMAFFxXKHiIkQO5cJ3Yhl5i6UPp+IhuteBJbuHcA5UogKo0EWtlWwW6KSaKoTNEYL7JlCQiVnkhBktUgg=="

	// Decode and parse the public key.
	publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKey)
	fmt.Printf("PUBLIC KEY BASE 64 BYTES: %x\n", publicKeyBytes)
	if err != nil {
		return fmt.Errorf("error decoding public key bytes: %v", err)
	}
	parsedPublicKeyUntyped, err := x509.ParsePKIXPublicKey(publicKeyBytes)
	parsedPublicKey := parsedPublicKeyUntyped.(*ecdsa.PublicKey)
	fmt.Printf("PARSED PUBLIC KEY X: %v, Y: %v\n", parsedPublicKey.X, parsedPublicKey.Y)
	if err != nil {
		return fmt.Errorf("error parsing public key: %v", err)
	}

	// Decode the signature.
	versionSignatureBytes, err := base64.StdEncoding.DecodeString(versionSignature)
	fmt.Printf("SIGNATURE BYTES: %x\n", versionSignatureBytes)
	if err != nil {
		return fmt.Errorf("error decoding signature bytes: %v", err)
	}

	// Verify the signature against the package's SHA256 sum.
	// presentSHA256Bytes := md5.Sum(compressedPackage.Bytes())
	presentSHA256Bytes := sha256.Sum256(compressedPackage.Bytes())
	fmt.Printf("PRESENT SHA256: %x\n", presentSHA256Bytes)
	if !ecdsa.VerifyASN1(parsedPublicKey, presentSHA256Bytes[:], versionSignatureBytes) {
		return fmt.Errorf("signature verification failed")
	}
	return nil
}

func main() {
	var compressedPackage bytes.Buffer
	if err := downloadPackage(&compressedPackage); err != nil {
		fmt.Printf("failed to download package: %v", err)
	}
	if err := verifyPackage(&compressedPackage); err != nil {
		fmt.Printf("failed to verify package: %v", err)
	}
}

Currently this outputs:

PUBLIC KEY BASE 64 BYTES: 3059301306072a8648ce3d020106082a8648ce3d03010703420004d4e95bdf3300145c572878889103b9709dd8865e62e943e9f8886eb5e0496ee1dc03952880aa34116b655b05ba29268aa1334460bec9942422567921064b5482
PARSED PUBLIC KEY X: 96302633342102462193322732537154456057293035750541189099858346903022946840289, Y: 99515156681666470022533827122417036293197994443229625834881400620927226172546
SIGNATURE BYTES: 304502210097ff8f5ab4d7920d864ff1b7ad61612a0741bccd99c8c81ee26c2ee744dc8c8902201e8b0e2878554d534123fea250d4a90e890e738e33b19ed37f26cc2a3f1875f6
PRESENT SHA256: a1b59bcdca7b8a6844c48fee7031842fcecec26fb028e4b5cbcb8c055ca0eeaa
failed to verify package: signature verification failed

Thank you for any help!

主要语言
MDX
星标
715
派生
4.2k
平均合并
6 天 10 小时
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

npm/documentation 的其他 Issue

查看 npm/documentation 的全部 Issue

相似的 Issue

更多 Cryptography Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。