|
@@ -18,13 +18,10 @@ import Foundation
|
|
extension Data {
|
|
extension Data {
|
|
/// Two octet checksum as defined in RFC-4880. Sum of all octets, mod 65536
|
|
/// Two octet checksum as defined in RFC-4880. Sum of all octets, mod 65536
|
|
public func checksum() -> UInt16 {
|
|
public func checksum() -> UInt16 {
|
|
- var s: UInt32 = 0
|
|
|
|
- let bytesArray = bytes
|
|
|
|
- for i in 0 ..< bytesArray.count {
|
|
|
|
- s = s + UInt32(bytesArray[i])
|
|
|
|
|
|
+ let s = self.withUnsafeBytes { buf in
|
|
|
|
+ return buf.lazy.map(UInt32.init).reduce(UInt32(0), +)
|
|
}
|
|
}
|
|
- s = s % 65536
|
|
|
|
- return UInt16(s)
|
|
|
|
|
|
+ return UInt16(s % 65535)
|
|
}
|
|
}
|
|
|
|
|
|
public func md5() -> Data {
|
|
public func md5() -> Data {
|