A function that generates a CID object for a given string and returns it. Uses SHA256 to hash the string and generate a multihash from it. The mulithash is then base58 encoded and then used to create the CID
(namestring string)
| 441 | // Uses SHA256 to hash the string and generate a multihash from it. |
| 442 | // The mulithash is then base58 encoded and then used to create the CID |
| 443 | func generateCID(namestring string) cid.Cid { |
| 444 | // Hash the service content ID with SHA256 |
| 445 | hash := sha256.Sum256([]byte(namestring)) |
| 446 | // Append the hash with the hashing codec ID for SHA2-256 (0x12), |
| 447 | // the digest size (0x20) and the hash of the service content ID |
| 448 | finalhash := append([]byte{0x12, 0x20}, hash[:]...) |
| 449 | // Encode the fullhash to Base58 |
| 450 | b58string := base58.Encode(finalhash) |
| 451 | |
| 452 | // Generate a Multihash from the base58 string |
| 453 | mulhash, err := multihash.FromB58String(string(b58string)) |
| 454 | if err != nil { |
| 455 | logrus.WithFields(logrus.Fields{ |
| 456 | "error": err.Error(), |
| 457 | }).Fatalln("Failed to Generate Service CID!") |
| 458 | } |
| 459 | |
| 460 | // Generate a CID from the Multihash |
| 461 | cidvalue := cid.NewCidV1(12, mulhash) |
| 462 | // Return the CID |
| 463 | return cidvalue |
| 464 | } |
no test coverage detected