How to generate list of set of numbers and generate sha256 in golang

I want to generate numbers (Set) like from 1 to 100 and get sha256 hash of each in separate Line.
Example :-
1 : 6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b
2 : d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35
.
.
.
.
100: ad57366865126e55649ecb23ae1d48887544976efea46a48eb5d85a6eeb4d306

please help me in Code So i can learn more for completing and golang project.

package main

import (
	"crypto/sha256"
	"fmt"
	"strconv"
)

func main() {
	for i := 1; i <= 100; i++ {
		hash := sha256.Sum256([]byte(strconv.Itoa(i)))
		fmt.Printf("%d. %x\n", i, hash)
	}
}

Playground

1 Like

Truly Amazing…Thanks acim (Boban Acimovic) . It works.

And here is a little play with goroutines :stuck_out_tongue:

Playground

But better try to run it on your computer to see the concurrency really.

1 Like

It gets jumbled up … like 100 coming first(good Learning) ,…,i like to also ask you that this is for numbers – But how to get for alphabets like a,b,c …to z to generate sha256??

Just a partial help this time, so you have to combine this with the previous (first code)

https://play.golang.org/p/j15IaOGiTWR

1 Like

thankyou …let me see .that i can find a way to implement sha256 in it.

1 Like

This totally depends!

Hashes are built from []byte, but neither 1, nor 100, nor anything inbetween is []byte, so you need to specify first how you want to convert them to []byte.

So until we know, how you want to represent the numbers (as string? LE or BE? Bit-Width? Signed or unsigned?) none of the proposed solutions is actually correct…

1 Like

i want -
for example
a ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
b 3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d
.
.
.
z 594e519ae499312b29433b7dd8a97ff068defcba9755b6d5d00e84c524d67b06

Yeah, for strings or single chars it is (nearly) a no-brainer. You have them already in a certain encoding and treat single chars as strings of length one and then convert them into a []byte.

But I am asking how you want to encode numbers. That is an important question.

Lets say we have []byte{0xff}, I can read this either as -1 or as 255, depending on if I read it into int8 or uint8.

Or []byte{0x00, 0xff} can be interpreted as little endian 16 bit signed integer (-256), little endian 16 bit unsigned integer (65280), big endian signed or unsigned 16 bit integer (255).

So counting the usual bit widths (8, 16, 32, 64), endiannes and signedness plus simply rendering numbers as plain strings, we have about 1 + 4 * 2 * 2 = 17 ways to represent the number 1 as []byte, even more if you count in floats…


To answer your question about letters… sha256.Sum256([]byte("a")) should be hint enough to actually finish the job.

1 Like

yes…But but the above [aicm gave for numbers program But i want a program which results from a to z and gives sha256]

Here’s a simple way using ascii codes:

package main

import (
	"crypto/sha256"
	"fmt"
)

func main() {
	for i := 97; i <= 122; i++ {
		hash := sha256.Sum256([]byte(string(i)))
		fmt.Printf("%s. %x\n", string(i), hash)
	}
}

But this could be achieved in many ways, for example defining an array of letters:

package main

import (
	"crypto/sha256"
	"fmt"
)

func main() {
	letters := []string{"a", "b", "c"} //Just three letters for simplicity.
	for _, letter := range letters {
		hash := sha256.Sum256([]byte(letter))
		fmt.Printf("%s. %x\n", letter, hash)
	}
}

The thing is, as @NobbZ mentioned, sha256.Sum256 is all that is needed, the rest is just representation.

1 Like

When you can’t combine @acim’s solution for numbers with my closing sentence after the horizontal ruler, you should take some steps back and start doing the go tour (again).

2 Likes

ok

Come on, I gave to the way how to loop thru ASCII as well, this is just 2 minutes to do now :smiley:

package main

import (
	"fmt"
)

func main() {
	for i := 1; i <= 26; i++ {
		fmt.Printf("%d %q\n", i, toChar(i))
	}
}

func toChar(i int) rune {
	return rune('a' - 1 + i)
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.