Created
September 29, 2020 21:10
-
-
Save wjlafrance/459d20eb6add423acf67805494d2bba8 to your computer and use it in GitHub Desktop.
FinaleBot_CheckRevision.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import CryptoSwift | |
enum CheckRevision { | |
enum CheckRevisionError: Error { | |
case formulaNotBase64 | |
case formulaTooShort | |
case unparseableVersion | |
} | |
static func execute(formula: String, versionString: String = "1.14.3.71") throws -> (UInt32, UInt32, String) { | |
guard let formulaData = Data(base64Encoded: formula, options: []) else { | |
throw CheckRevisionError.formulaNotBase64 | |
} | |
guard formulaData.count > 4 else { | |
throw CheckRevisionError.formulaTooShort | |
} | |
let versionComponents = versionString.split(separator: ".").compactMap({ UInt32($0) }) | |
guard versionComponents.count == 4 else { throw CheckRevisionError.unparseableVersion } | |
let version = versionComponents[3] << 24 | versionComponents[2] << 16 | versionComponents[1] << 8 | versionComponents[0] | |
var hashComposer = RawMessageComposer() | |
var formulaConsumer = RawMessageConsumer(message: formulaData) | |
hashComposer.write(formulaConsumer.readUInt32()) | |
hashComposer.write(":\(versionString):", withNullTerminator: false) | |
hashComposer.write(1 as UInt8) | |
var hashConsumer = RawMessageConsumer(message: hashComposer.build().sha1().base64EncodedData(options: [])) | |
let hash = hashConsumer.readUInt32() | |
let info = hashConsumer.readString(length: hashConsumer.message.count - 4) | |
return (version, hash, info) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment