tranxuanthang / lrcget

Utility for mass-downloading LRC synced lyrics for your offline music library.
MIT License
759 stars 24 forks source link

publish new lyrics #109

Open JohnStyleZ opened 3 months ago

JohnStyleZ commented 3 months ago

I tried to convert the rust file into python and integrate this into my Flask application. but i am keep getting {"message":"The provided publish token is incorrect","name":"IncorrectPublishTokenError","statusCode":400}.

can you take a look if i done something wrong?

def verify_nonce(result, target): if len(result) != len(target): return False

for i in range(len(result)):
    if result[i] > target[i]:
        return False
    elif result[i] < target[i]:
        break

return True

def solve_challenge(prefix, target_hex): nonce = 0 target = bytes.fromhex(target_hex)

while True:
    token = f"{prefix}:{nonce}".encode()
    hash_result = hashlib.sha256(token).digest()

    if verify_nonce(hash_result, target):
        break

    nonce += 1

return nonce

here is an example

{'prefix': '4FriQWe2ZRUVTrd8HoV6ZxZ4QdkDZHdI', 'target': '000000FF00000000000000000000000000000000000000000000000000000000'}

x-publish-token: 4FriQWe2ZRUVTrd8HoV6ZxZ4QdkDZHdI:35093022

DataM0del commented 6 days ago

I tried to convert the rust file into python and integrate this into my Flask application. but i am keep getting {"message":"The provided publish token is incorrect","name":"IncorrectPublishTokenError","statusCode":400}.

can you take a look if i done something wrong?

def verify_nonce(result, target): if len(result) != len(target): return False

for i in range(len(result)):
    if result[i] > target[i]:
        return False
    elif result[i] < target[i]:
        break

return True

def solve_challenge(prefix, target_hex): nonce = 0 target = bytes.fromhex(target_hex)

while True:
    token = f"{prefix}:{nonce}".encode()
    hash_result = hashlib.sha256(token).digest()

    if verify_nonce(hash_result, target):
        break

    nonce += 1

return nonce

here is an example

{'prefix': '4FriQWe2ZRUVTrd8HoV6ZxZ4QdkDZHdI', 'target': '000000FF00000000000000000000000000000000000000000000000000000000'}

x-publish-token: 4FriQWe2ZRUVTrd8HoV6ZxZ4QdkDZHdI:35093022

  1. python bad die
  2. can you please fix your issue description? The codeblocks aren't properly formatted, also, please add the language name after the three ticks (e.g. py/python, rs/rust, cpp/c++)
  3. I suck at cryptography but I don't care I'm going to try and fix it anyway lol
DataM0del commented 5 days ago

@JohnStyleZ Codeium fixed it :tm: I'll upload the translation of the server token validator & generator to python soon in a second.

import hashlib
import requests
# was just testing, didn't want to put strain on the official lrclib server,
# so I just got codeium to rewrite it in python.

def verify_answer(prefix: str, target: str, nonce: int):
    input_str = prefix + str(nonce)
    hashed_bytes = hashlib.sha256(input_str.encode()).digest()
    target_bytes = bytes.fromhex(target)

    if len(target_bytes) != len(hashed_bytes):
        return False

    for hashed_byte, target_byte in zip(hashed_bytes, target_bytes):
        if hashed_byte > target_byte:
            return False
        if hashed_byte < target_byte:
            break

    return True

def solve_challenge(prefix, target_hex):
    """
    Solve the challenge by iterating through possible nonce values until a valid one is found.

    Args:
        prefix (str): The prefix of the token to be hashed.
        target_hex (str): The target hash value as a hexadecimal string.

    Returns:
        int: The nonce value that satisfies the challenge.
    """
    nonce = 0
    target = bytes.fromhex(target_hex)
    print("Solving challenge...")

    while True:
        token = f"{prefix}:{nonce}".encode()
        # print("Generated token:", token)

        if verify_answer(prefix, target.hex(), nonce):
            break

        # print("Nonce not valid, incrementing...")

        nonce += 1

    print("Nonce found:", nonce)

    return nonce

res = requests.post("https://lrclib.net/api/request-challenge")
json = res.json()

solution = solve_challenge(json["prefix"], json["target"])
token = f"{json['prefix']}:{solution}"
print("Token:", token)
# do something with the token
DataM0del commented 5 days ago

@JohnStyleZ uploaded it to Codeberg: https://codeberg.org/RealPacket/lrclib-challenge-solver-python/ (this account really is only for contributing to GitHub things, don't trust them with my main repos anymore after they suspended me out of nowhere, and I don't know why because I forgot to update my main email on GitHub to a different one because it was using skiff, and I was suspended way after August 9, 2024, so I can't view the skiff emails, I didn't have forwarding on either)