Fetch Marvel API Heroes with MD5 Hash

Hello, Developers.


I Need to fetch this API from Marvel Heroes using Swift 5 - MD5 or some type like that.


Marvel develper: https://developer.marvel.com

Authentication for Server-Side Applications

Server-side applications must pass two parameters in addition to the apikey parameter:

  • ts - a timestamp (or other long string which can change on a request-by-request basis)
  • hash - a md5 digest of the ts parameter, your private key and your public key (e.g. md5(ts+privateKey+publicKey)


For example, a user with a public key of "1234" and a private key of "abcd" could construct a valid call as follows:

http://gateway.marvel.com/v1/public/comics?ts=1&apikey=1234&hash=ffd275c5130566a2916217b101f26150
(the hash value is the md5 digest of 1abcd1234)


And,

for a example only - I did it with JavaScript:


// Just a part of code... 
...
import md5 from 'js-md5';

// Get yours APIs key at https://developer.marvel.com
const PUBLIC_KEY = ''; // your public key
const PRIVATE_KEY = ''; // youur private key

export default function Heroes({ navigation }) {
  const [feedHeroes, setFeedHeroes] = useState([]);

  useEffect(() => {
      async function loadHeroesCharacters() {
        const ts = Number(new Date());
        const hash = md5.create();
        hash.update(ts + PRIVATE_KEY + PUBLIC_KEY);
        try {
          const response = await fetch(
            `https://gateway.marvel.com/v1/public/characters?ts=${ts}&orderBy=name&limit=10&apikey=${PUBLIC_KEY}&hash=${hash.h ex()}`
          );
// continue
  ...


From now on, I really need to make the same thing with Swift 5. I need to use URLSession and should not to use a another dependecies (CocoaPods) like Alomofire to fetch this API.

But how I use the MD5 - Hashing with Swift 5?


Is there somebody who could help me, please?


Cheers,

Bruno.

Replies

> ..how I use the MD5 - Hashing with Swift 5?


Apologies if this is over your getting started threshold, but have you seen this SO thread?


https://stackoverflow.com/questions/32163848/how-can-i-convert-a-string-to-an-md5-hash-in-ios-using-swift