-1

I wanted to give encryption to a string before storing it in smart contract.

For example I have "hello" its 256 equivalent should be "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

Please note that both input and output should be of type sp.TString

Below is a failed attempt to do so. It will help you understand what I am trying to do.

import smartpy as sp
class Try(sp.Contract):
    def __init__(self):
        self.init(mystr = sp.string(""))

    @sp.entry_point
    def encypt(self,params):
        self.data.mystr =  sp.sha256(params.key)


@sp.add_test(name="try")
def test():
    obj = Try()
    scenario = sp.test_scenario()
    scenario += obj
    scenario += obj.encypt(key="hello")

Thanks in advance.

  • What is your error ? Please offer all the details as people cannot be expected to infer what is the problem your are facing. – Ezy Mar 2 at 9:58
  • I want to convert a string to its sha256 equivalent, that too in string format. – Pawan Dhanwani Mar 2 at 10:53
  • You have not indicated the error you are getting so your question is incomplete. Please follow the guidelines of TSE, thank you. – Ezy Mar 3 at 10:29
1

Both the input and output of a hash function is a bytes in Michelson.

There is a hack for the input, you can do something like that.

import smartpy as sp

class MyContract(sp.Contract):
    def __init__(self):
        self.init(x = sp.sha256(sp.slice(sp.pack("hello"), 6, 5).open_some()))

@sp.add_test(name = "")
def test():
    scenario = sp.test_scenario()
    c1 = MyContract()
    scenario += c1

https://smartpy.io/demo/index.html?code=eJxVT7sKwzAM3P0VIpNFS6CFZigECp37DUY4CjF1bGN5aP6@dh_Qajrdne6QW1PMBWSlXNIGJCBJKetJBG7bNYaSyRYtqf9iPCuoM_EMxrjgijFa2M8fvk1b@ybpB4w1sJeFjqehpYh3lhtIZO@6W9j72OEehj2csI@Jg5G4skZEpS7VR9NkCkvRgVauaV2HqnW_uE@nWA6UXXyXNcV8KY0vhz1U7ecf_L_bjdWhnvd1Upo-

You can write another hack for the output as well. Do we really need this?

| improve this answer | |
  • If you don't think we need this, can you provide an alternative for storing encrypted data on the blockchain/contract storage? – utdrmac Mar 2 at 15:12
  • Store bytes obviously as everyone is doing. Do not bother to convert to string for no clear reasons. This is what is naturally done in Michelson. – FFF Mar 2 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.