user27636
user27636

Reputation: 31

Hash a vector of e.g. strings to obtain one-by-one sha256 vector of hashes (Python)

I wondered if there existed a Python function f which calculates the individual sha256 number of a vector at the same time. E.g. if you have a vector A=[a b], the function f gives f(A) = [sha256(a) sha256(b)] (a and b are strings or numbers).

I can't make it work with hashlib. In addition, the package 'openssl' from R has such function 'sha256'. Is there an equivalence?

I'm using Spyder IDE.

Thanks.

Upvotes: 1

Views: 353

Answers (1)

rioV8
rioV8

Reputation: 28763

A = [a,b]
Asha = list(map(sha256, A))

or

def sha256List(arr):
   return list(map(sha256, arr))

Asha = sha256List(A)

Edit

If you want a list with the hexdigest values you can use

def sha256HexDigestList(arr):
   return list(map(lambda x: sha256(x).hexdigest(), arr))

Asha_digest = sha256HexDigestList(A)

Upvotes: 3

Related Questions