Discuss Scratch

-EmeraldThunder-
Scratcher
1000+ posts

Project.json variable creation.

I've been trying to implement variables into a side project of mine and have been trying to get these appended into the json on compile.
I have found the particular object which holds variables in the json.
"variables":{"P/T!j8/7Eg]MOO:-Tw/q":["test",0]}
How can I generate the hash that is the key of the variable array?

Edit:
Looks like the key is just a ranodm id. It would still be nice to generate one without a big long list of characters.

Last edited by -EmeraldThunder- (Nov. 6, 2021 12:15:04)

Chiroyce
Scratcher
1000+ posts

Project.json variable creation.

-EmeraldThunder- wrote:

that is the key of the variable array?
Which would be

-EmeraldThunder- wrote:

“variables”:{"P/T!j8/7Eg]MOO:-Tw/q":}
the underlined, right?

then —
# Assuming that you're using Python
import hashlib
hash = 'P/T!j8/7Eg]MOO:-Tw/q'.encode()  # to convert a string to bytes
hash = hashlib.sha256(hash).hexdigest()
print(hash)

Is this what you want?

Last edited by Chiroyce (Nov. 6, 2021 11:27:10)

-EmeraldThunder-
Scratcher
1000+ posts

Project.json variable creation.

Chiroyce wrote:

Is this what you want?
No I want to be able to just generate a random valid hash.
Chiroyce
Scratcher
1000+ posts

Project.json variable creation.

-EmeraldThunder- wrote:

random valid hash
erm … is it this?


or am I missing something?
-EmeraldThunder-
Scratcher
1000+ posts

Project.json variable creation.

Chiroyce wrote:

-EmeraldThunder- wrote:

random valid hash
or am I missing something?
Sorry that's not what I meant.
Is there a library oir something that will let me generate a 20 character hash similar to the one under targets.varaibles. I really don't want to have to create a list of file will all the characters in it.

Last edited by -EmeraldThunder- (Nov. 6, 2021 12:10:15)

Chiroyce
Scratcher
1000+ posts

Project.json variable creation.

-EmeraldThunder- wrote:

Is there a library oir something that will let me generate a 20 character hash similar to the one under targets.varaibles. I really don't want to have to create a list of file will all the characters in it.
hmm … since scratch is written in JavaScript, it may not be very easy. Why don't you go around Scratch's GitHub code to see how it generates the hash

Last edited by Chiroyce (Nov. 6, 2021 12:21:46)

-EmeraldThunder-
Scratcher
1000+ posts

Project.json variable creation.

Wrote this with a little bit of help from co-pilot to condense the for loop in line 6.
import random
import string
import base64
def rand_b64(length):
    data = ''.join(random.choice(string.ascii_letters) for _ in range(length)).encode('utf-8')
    return str(base64.b64encode(data), "utf-8")
print(rand_b64(15))
Reported to be closed as resolved.

Last edited by -EmeraldThunder- (Nov. 6, 2021 12:35:39)

Maximouse
Scratcher
1000+ posts

Project.json variable creation.

-EmeraldThunder- wrote:

Wrote this with a little bit of help from co-pilot to condense the for loop in line 6.
import random
import string
import base64
def rand_b64(length):
    data = ''.join(random.choice(string.ascii_letters) for _ in range(length)).encode('utf-8')
    return str(base64.b64encode(data), "utf-8")
print(rand_b64(15))
Reported to be closed as resolved.
There is no reason to encode letters as base64 – it just makes the string longer.
Harakou
Scratcher
1000+ posts

Project.json variable creation.

Closed by request of owner.

Powered by DjangoBB