php小編西瓜在這裡為大家介紹如何使用Python檢查sum.golang.org中的go.mod雜湊值。 sum.golang.org是一個用於驗證Go模組的雜湊值的官方服務,它可以幫助開發者確保模組的完整性和安全性。透過使用Python的requests函式庫和hashlib函式庫,我們可以輕鬆地取得並比對go.mod檔的雜湊值,從而確保我們使用的模組是可信的。下面就讓我們一起來看看具體的實作步驟吧。
問題內容
我需要驗證 sum.golang.org 提供的 go.mod 檔案的雜湊值。我需要使用 PYTHON。
例如- https://sum.golang.org/lookup/github.com/gin-gonic/[電子郵件受保護]檔案https://proxy.golang.org/github. com/gin-gonic/gin/@v/v1.6.2.mod
#我們在這裡:
import base64 import requests import hashlib import os # some tmp file tmp_file = os.path.abspath(os.path.dirname(__file__)) + '/tmp.mod' # url for sumdb link_sum_db = 'https://sum.golang.org/lookup/github.com/gin-gonic/[email protected]' # our line: # github.com/gin-gonic/gin v1.6.2/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= hash_from_sumdb = b'75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=' print(hash_from_sumdb) # b'75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=' # download the file f_url = 'https://proxy.golang.org/github.com/gin-gonic/gin/@v/v1.6.2.mod' f_url_content = requests.get(f_url).content with open(tmp_file, 'wb') as f: f.write(f_url_content) with open(tmp_file, 'rb') as f: f_file_content = f.read() # calculate hash from local tmp file hash_from_file = base64.b64encode(hashlib.sha256(f_file_content).digest()) print(hash_from_file) # b'x9T1RkIbnNSJydQMU9l8mvXfhBIkDhO3TTHCbOVG4Go=' # and it fails =( assert hash_from_file == hash_from_sumdb
請幫幫我。我知道 go
命令,但我需要在這裡使用 python ...
我讀過這個主題,但沒有幫助 =(
解決方法
事情似乎比這更複雜一些。 我關注了您提到的主題,並在 這個答案。 另外,如果您參考 該函數原始碼,您可以看到go模組中使用的hash是如何實現的。
此版本有效:
import hashlib import base64 def calculate_sha256_checksum(data): sha256_hash = hashlib.sha256() sha256_hash.update(data.encode('utf-8')) return sha256_hash.digest() # Specify the file path file_path = 'go.mod' # Read the file content with open(file_path, 'r') as file: file_content = file.read() # Calculate the SHA256 checksum of the file content checksum1 = calculate_sha256_checksum(file_content) # Format the checksum followed by two spaces, filename, and a new line formatted_string = f'{checksum1.hex()} {file_path}\n' # Calculate the SHA256 checksum of the formatted string checksum2 = calculate_sha256_checksum(formatted_string) # Convert the checksum to base64 base64_checksum = base64.b64encode(checksum2).decode('utf-8') print(base64_checksum)
以上是如何使用 Python 檢查 sum.golang.org 中的 go.mod 雜湊值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C 更適合需要直接控制硬件資源和高性能優化的場景,而Golang更適合需要快速開發和高並發處理的場景。 1.C 的優勢在於其接近硬件的特性和高度的優化能力,適合遊戲開發等高性能需求。 2.Golang的優勢在於其簡潔的語法和天然的並發支持,適合高並發服務開發。

Golang在实际应用中表现出色,以简洁、高效和并发性著称。1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...

golang ...

Go語言中如何對比並處理三個結構體在Go語言編程中,有時需要對比兩個結構體的差異,並將這些差異應用到第�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1
好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版
中文版,非常好用