crc8.swift 536 Bytes
//
//  crc8.swift
//  Twear
//
//  Created by yangbin on 2021/12/8.
//

import UIKit

func crc8(bytes: [UInt8]) -> UInt8 {
    if bytes.count == 0 {
        return 0x00
    }
    let len = bytes.count
//    let bytes = [UInt8](data)
    var temp: UInt8 = 0xFF
//    let len = data!.count
    for i in 0..<len {
        temp ^= bytes[i] & 0xFF
        for _ in 0..<8 {
            if temp & 0x1 != 0 {
                temp = temp >> 1^0xB8
            } else {
                temp >>= 1
            }
        }
    }
    return temp
}