-2

I want to create connections with banks and as I see in the documentation of cardlink they want to calculate digest.

The main idea of this calculation is this

Digest=base64( sha-1( utf8bytes(value1|value2|...|secret) ) )

.

MessageDigestmdigest = MessageDigest.getInstance("SHA-1");
byte[] digestResult =digest.digest(concatString.getBytes("UTF-8"));
String calculatedDigest = Base64.encode(digestResult);

This is the code they have for java.

I started for testing this manually but I stucked with the convertion of bytes.

this is the finall step for base64

this is for sha-1

So my main question is how can I do the conversion to bytes and if It would help a lot if someone knows a way to do it with swift (ios).

Thanks in advance

EDIT

This is the example string I got:

2.045020311114033331503343429780020454510MDAwMDAwMDAwMDAwMDA2ODkzOTI=https://myeshop.gr/orders/Payment_Okhttps://myeshop.gr/orders/Payment_FailEURO123

And this is the result I want:

wXRSbgCX2Kq6gSOVE6+c9VpvSRQ=
mike vorisis
  • 2,786
  • 6
  • 40
  • 74
  • take a look of this post http://stackoverflow.com/questions/25761344/how-to-crypt-string-to-sha1-with-swift – Enix Oct 31 '16 at 14:14
  • http://stackoverflow.com/a/34190615/341994 – matt Oct 31 '16 at 14:14
  • http://stackoverflow.com/questions/26970807/implementing-hmac-and-sha1-encryption-in-swift, http://stackoverflow.com/questions/24099520/commonhmac-in-swift – Martin R Oct 31 '16 at 14:23
  • thanks for the comments @Enix this returns me the sha-1 that I already have (I'm keeping it for later use) matt These numbers I got didn't gave the result I wanted with base64 and Martin R The first you gave me doesn't gave the result I wanted now I'm checking the second one – mike vorisis Oct 31 '16 at 14:31
  • Show the code that you tried, together with input data, expected output and actual output. Otherwise we can only *guess* what the problem is. – Martin R Oct 31 '16 at 14:38
  • @MartinR all these that you send me are correct and they returned me the same string as the sha1 site I have in my question. But the thing I stucked is the convertion to bytes. – mike vorisis Oct 31 '16 at 14:45
  • This one http://stackoverflow.com/a/26971119/1187415 and this one http://stackoverflow.com/a/27032056/1187415 compute a SHA digest and encode the result as Base64. Isn't that what you are looking for? – Martin R Oct 31 '16 at 14:54
  • @mikevorisis you mean you already have `calculatedDigest`, and need to convert it to bytes? – Enix Oct 31 '16 at 15:04
  • @MartinR there is another step with conversion the string to bytes then to sha and finally to base64 (maybe I'm getting this wrong) but I see this Digest=base64( sha-1( utf8bytes(value1|value2|...|secret) ) ) and from that I telling this thought – mike vorisis Oct 31 '16 at 15:09
  • @Enix no to calculate the digest I need 3 conversions as I have understood 1. the first string I got to bytes this with sha-1 and then base64 – mike vorisis Oct 31 '16 at 15:11
  • The string to UTF-8 conversion is already done in the method in http://stackoverflow.com/a/26971119/1187415 with `self.dataUsingEncoding(NSUTF8StringEncoding, ...)`. Again: Please *show* the Swift code that you tried in your question, together with input, and actual/expected output. – Martin R Oct 31 '16 at 15:14
  • @MartinR I updated my question – mike vorisis Oct 31 '16 at 15:15
  • What is unclear about *"Show the code that you tried, together with input data, expected output and actual output."*? – I am giving up ... – Martin R Oct 31 '16 at 15:17
  • @MartinR I tried solutions that all of you provided me I can't update my question with all of these (they are extensions I simple put them in my project and used them) – mike vorisis Oct 31 '16 at 15:21

1 Answers1

2

Is this what you need?

import UIKit

extension String {
    func sha1() -> String {
        let data = self.dataUsingEncoding(NSUTF8StringEncoding)!
        var digest = [UInt8](count:Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0)
        CC_SHA1(data.bytes, CC_LONG(data.length), &digest)
        let hexBytes = digest.map { String(format: "%02hhx", $0) }
        return hexBytes.joinWithSeparator("")
    }

    func sha1Data() -> NSData {
        let data = self.dataUsingEncoding(NSUTF8StringEncoding)!
        var digest = [UInt8](count:Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0)
        CC_SHA1(data.bytes, CC_LONG(data.length), &digest)
        let digestData = NSData(bytes: digest, length: digest.count)
        return digestData
    }
}


class TestSHA1Digest: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let b64 = calculateDigest("2.045020311114033331503343429780020454510MDAwMDAwMDAwMDAwMDA2ODkzOTI=https://myeshop.gr/orders/Payment_Okhttps://myeshop.gr/orders/Payment_FailEURO123") {
            print("SHA1: \(b64)")
        } else {
            print("Calcuate SHA1 failed")
        }
    }

    func calculateDigest(data: String) -> String? {
        let shaData = data.sha1Data()
        return shaData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
    }
}

[Output] SHA1: wXRSbgCX2Kq6gSOVE6+c9VpvSRQ=

PS. I am using swift 2 + Xcode 7.3.1. Please let me know if you are using swift 3.

Enix
  • 4,415
  • 1
  • 24
  • 37