|
@@ -22,7 +22,7 @@ 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
|
|
var s:UInt32 = 0
|
|
- var bytesArray = self.arrayOfBytes()
|
|
|
|
|
|
+ var bytesArray = self.bytes
|
|
for i in 0..<bytesArray.count {
|
|
for i in 0..<bytesArray.count {
|
|
s = s + UInt32(bytesArray[i])
|
|
s = s + UInt32(bytesArray[i])
|
|
}
|
|
}
|
|
@@ -31,72 +31,69 @@ extension Data {
|
|
}
|
|
}
|
|
|
|
|
|
public func md5() -> Data {
|
|
public func md5() -> Data {
|
|
- let result = Hash.md5(self.arrayOfBytes()).calculate()
|
|
|
|
|
|
+ let result = Hash.md5(self.bytes).calculate()
|
|
return Data(bytes: result)
|
|
return Data(bytes: result)
|
|
}
|
|
}
|
|
|
|
|
|
public func sha1() -> Data? {
|
|
public func sha1() -> Data? {
|
|
- let result = Hash.sha1(self.arrayOfBytes()).calculate()
|
|
|
|
|
|
+ let result = Hash.sha1(self.bytes).calculate()
|
|
return Data(bytes: result)
|
|
return Data(bytes: result)
|
|
}
|
|
}
|
|
|
|
|
|
public func sha224() -> Data? {
|
|
public func sha224() -> Data? {
|
|
- let result = Hash.sha224(self.arrayOfBytes()).calculate()
|
|
|
|
|
|
+ let result = Hash.sha224(self.bytes).calculate()
|
|
return Data(bytes: result)
|
|
return Data(bytes: result)
|
|
}
|
|
}
|
|
|
|
|
|
public func sha256() -> Data? {
|
|
public func sha256() -> Data? {
|
|
- let result = Hash.sha256(self.arrayOfBytes()).calculate()
|
|
|
|
|
|
+ let result = Hash.sha256(self.bytes).calculate()
|
|
return Data(bytes: result)
|
|
return Data(bytes: result)
|
|
}
|
|
}
|
|
|
|
|
|
public func sha384() -> Data? {
|
|
public func sha384() -> Data? {
|
|
- let result = Hash.sha384(self.arrayOfBytes()).calculate()
|
|
|
|
|
|
+ let result = Hash.sha384(self.bytes).calculate()
|
|
return Data(bytes: result)
|
|
return Data(bytes: result)
|
|
}
|
|
}
|
|
|
|
|
|
public func sha512() -> Data? {
|
|
public func sha512() -> Data? {
|
|
- let result = Hash.sha512(self.arrayOfBytes()).calculate()
|
|
|
|
|
|
+ let result = Hash.sha512(self.bytes).calculate()
|
|
return Data(bytes: result)
|
|
return Data(bytes: result)
|
|
}
|
|
}
|
|
|
|
|
|
public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> Data? {
|
|
public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> Data? {
|
|
- let result = Hash.crc32(self.arrayOfBytes(), seed: seed, reflect: reflect).calculate()
|
|
|
|
|
|
+ let result = Hash.crc32(self.bytes, seed: seed, reflect: reflect).calculate()
|
|
return Data(bytes: result)
|
|
return Data(bytes: result)
|
|
}
|
|
}
|
|
|
|
|
|
public func crc16(seed: UInt16? = nil) -> Data? {
|
|
public func crc16(seed: UInt16? = nil) -> Data? {
|
|
- let result = Hash.crc16(self.arrayOfBytes(), seed: seed).calculate()
|
|
|
|
|
|
+ let result = Hash.crc16(self.bytes, seed: seed).calculate()
|
|
return Data(bytes: result)
|
|
return Data(bytes: result)
|
|
}
|
|
}
|
|
|
|
|
|
public func encrypt(cipher: Cipher) throws -> Data {
|
|
public func encrypt(cipher: Cipher) throws -> Data {
|
|
- let encrypted = try cipher.encrypt(self.arrayOfBytes())
|
|
|
|
|
|
+ let encrypted = try cipher.encrypt(self.bytes)
|
|
return Data(bytes: encrypted)
|
|
return Data(bytes: encrypted)
|
|
}
|
|
}
|
|
|
|
|
|
public func decrypt(cipher: Cipher) throws -> Data {
|
|
public func decrypt(cipher: Cipher) throws -> Data {
|
|
- let decrypted = try cipher.decrypt(self.arrayOfBytes())
|
|
|
|
|
|
+ let decrypted = try cipher.decrypt(self.bytes)
|
|
return Data(bytes: decrypted)
|
|
return Data(bytes: decrypted)
|
|
}
|
|
}
|
|
|
|
|
|
public func authenticate(with authenticator: Authenticator) throws -> Data {
|
|
public func authenticate(with authenticator: Authenticator) throws -> Data {
|
|
- let result = try authenticator.authenticate(self.arrayOfBytes())
|
|
|
|
|
|
+ let result = try authenticator.authenticate(self.bytes)
|
|
return Data(bytes: result)
|
|
return Data(bytes: result)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
extension Data {
|
|
extension Data {
|
|
-
|
|
|
|
- public func toHexString() -> String {
|
|
|
|
- return self.arrayOfBytes().toHexString()
|
|
|
|
|
|
+
|
|
|
|
+ public var bytes: Array<UInt8> {
|
|
|
|
+ return Array(self)
|
|
}
|
|
}
|
|
|
|
|
|
- public func arrayOfBytes() -> Array<UInt8> {
|
|
|
|
- let count = self.count / sizeof(UInt8)
|
|
|
|
- var bytesArray = Array<UInt8>(repeating: 0, count: count)
|
|
|
|
- (self as NSData).getBytes(&bytesArray, length:count * sizeof(UInt8))
|
|
|
|
- return bytesArray
|
|
|
|
|
|
+ public func toHexString() -> String {
|
|
|
|
+ return self.bytes.toHexString()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|