How to check go.mod hashes in sum.golang.org using Python?
php editor Xigua is here to introduce how to use Python to check the go.mod hash value in sum.golang.org. sum.golang.org is an official service for verifying Go module hashes, which helps developers ensure the integrity and security of their modules. By using Python's requests library and hashlib library, we can easily obtain and compare the hash value of the go.mod file to ensure that the module we use is trustworthy. Let us take a look at the specific implementation steps below.
Question content
I need to verify the hash of the go.mod file provided by sum.golang.org. I need to use PYTHON.
For example - https://sum.golang.org/lookup/github.com/gin-gonic/[email protected]File https://proxy.golang.org/github. com/gin-gonic/gin/@v/v1.6.2.mod
We are here:
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
please help me. I know the go
command but I need to use python here...
I've read this thread but it didn't help =(
WORKAROUND
Things seem to be a little more complicated than that. I followed the topic you mentioned and found this answer. In addition, if you refer to The source code of this function, you can see how the hash used in the go module is implemented.
This version is valid:
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)
The above is the detailed content of How to check go.mod hashes in sum.golang.org using Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Since its inception in 2009, Bitcoin has become a leader in the cryptocurrency world and its price has experienced huge fluctuations. To provide a comprehensive historical overview, this article compiles Bitcoin price data from 2009 to 2025, covering major market events, changes in market sentiment, and important factors influencing price movements.

1. First, right-click on the blank space of the taskbar at the bottom of Windows 11 and select [Taskbar Settings]. 2. Find [taskbarcorneroverflow] on the right in the taskbar settings. 3. Then find [clock] or [Clock] above it and select to turn it on. Method 2: 1. Press the keyboard shortcut [win+r] to call up run, enter [regedit] and press Enter to confirm. 2. Open the registry editor, find [HKEY_CURRENT_USERControlPanel] in it, and delete it. 3. After deletion, restart the computer and you will be prompted for configuration. When you return to the system, the time will be displayed.

How do I use other people's Python code? Find code repositories: Find the code you need on platforms like PyPI and GitHub. Installation code: Use pip or clone the GitHub repository to install. Import modules: Use the import statement in your script to import installed modules. Working with code: Access functions and classes in modules. (Optional) Adapt the code: Modify the code as needed to fit your project.

Bitcoin, as a cryptocurrency, has experienced significant market volatility since its inception. This article will provide an overview of the historical price of Bitcoin since its birth to help readers understand its price trends and key moments. By analyzing Bitcoin's historical price data, we can understand the market's assessment of its value, factors affecting its fluctuations, and provide a basis for future investment decisions.

Since its creation in 2009, Bitcoin’s price has experienced several major fluctuations, rising to $69,044.77 in November 2021 and falling to $3,191.22 in December 2018. As of December 2024, the latest price has exceeded $100,204.

Real-time Bitcoin USD Price Factors that affect Bitcoin price Indicators for predicting future Bitcoin prices Here are some key information about the price of Bitcoin in 2018-2024:

Important Node for Bitcoin Historical Price January 3, 2009: Genesis Block was generated, the first Bitcoin was generated, with a value of USD 0. October 5: The first Bitcoin transaction, a programmer bought two pizzas with 10,000 bitcoins, equivalent to $0.008. February 9, 2010: The Mt. Gox exchange went online and became the main platform for early Bitcoin trading. May 22: Bitcoin breaks through $1 for the first time. July 17: Bitcoin price plunged to $0.008, hitting an all-time low. February 9, 2011: Bitcoin price breaks through $10 for the first time. April 10: Mt. Go

"ajavascript error occurred" indicates a JavaScript code execution error. Resolution steps include checking console logs, reviewing code, checking variable definitions, checking function calls, narrowing down code, using a debugger, searching for code snippets, and updating the browser or environment.
