1

I am attempting to write an authenticator app where I use SHA512 and 256 to encrypt some data and then display a number from the processed hashes. I have basically everything figured out and working except for the .digest function. I know how to do it in ruby, given below, but I can't figure out how to get it in iOS.

ruby:

  def reset_secret
    d = OpenSSL::Digest::SHA512.new
    d << reset_sysid
    d << 'AAaaAAAaaaAAA'
  end

  def reset_input(t)
    [reset_sysid, email, t].join('|')
  end

  def reset_hmac(t)
    hmac = OpenSSL::Digest::SHA256.new
    hmac << reset_secret.digest
    hmac << reset_input(t)
    hmac.digest
    OpenSSL::Digest::SHA256.new(hmac.digest).digest
  end

Swift as of now:

func reset_secret()->String {
    return (sysid+"AAaaAAAaaaAAA").sha512()
}

func reset_input(t:Int)->String{
    var time:String = String(t)
    var input:[String] = [sysid, email, time]
    var stringrep:String = "|".join(input)
    return stringrep
}

func reset_hmac(t:Int)->String{
    var firstTime:String = (reset_secret() + reset_input(t)).sha256()
    return firstTime
}

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

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

To start, I need to get a .digest of the reset_secret() in reset_hmac(), but I haven't seemed to find an equivalent to this in swift

seanscal
  • 568
  • 2
  • 9
  • 33
  • http://stackoverflow.com/questions/25388747/sha256-in-swift – 7stud Nov 04 '15 at 21:07
  • @7stud do you know what is different about that answer? Is that more like .digest than this method? How would I get one of the bytes out of the returned data? – seanscal Nov 05 '15 at 15:00

0 Answers0