|
@@ -14,7 +14,8 @@
|
|
|
//
|
|
|
|
|
|
extension Array {
|
|
|
- init(reserveCapacity: Int) {
|
|
|
+
|
|
|
+ public init(reserveCapacity: Int) {
|
|
|
self = Array<Element>()
|
|
|
self.reserveCapacity(reserveCapacity)
|
|
|
}
|
|
@@ -22,24 +23,7 @@ extension Array {
|
|
|
var slice: ArraySlice<Element> {
|
|
|
return self[self.startIndex..<self.endIndex]
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-extension Array {
|
|
|
-
|
|
|
- /// split in chunks with given chunk size
|
|
|
- @available(*, deprecated: 0.8.0, message: "")
|
|
|
- public func chunks(size chunksize: Int) -> Array<Array<Element>> {
|
|
|
- var words = Array<Array<Element>>()
|
|
|
- words.reserveCapacity(count / chunksize)
|
|
|
- for idx in stride(from: chunksize, through: count, by: chunksize) {
|
|
|
- words.append(Array(self[idx - chunksize..<idx])) // slow for large table
|
|
|
- }
|
|
|
- let remainder = suffix(count % chunksize)
|
|
|
- if !remainder.isEmpty {
|
|
|
- words.append(Array(remainder))
|
|
|
- }
|
|
|
- return words
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
extension Array where Element == UInt8 {
|
|
@@ -81,4 +65,84 @@ extension Array where Element == UInt8 {
|
|
|
append(b)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public func toHexString() -> String {
|
|
|
+ return `lazy`.reduce("") {
|
|
|
+ var s = String($1, radix: 16)
|
|
|
+ if s.count == 1 {
|
|
|
+ s = "0" + s
|
|
|
+ }
|
|
|
+ return $0 + s
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+extension Array where Element == UInt8 {
|
|
|
+
|
|
|
+ /// split in chunks with given chunk size
|
|
|
+ @available(*, deprecated: 0.8.0, message: "")
|
|
|
+ public func chunks(size chunksize: Int) -> Array<Array<Element>> {
|
|
|
+ var words = Array<Array<Element>>()
|
|
|
+ words.reserveCapacity(count / chunksize)
|
|
|
+ for idx in stride(from: chunksize, through: count, by: chunksize) {
|
|
|
+ words.append(Array(self[idx - chunksize..<idx])) // slow for large table
|
|
|
+ }
|
|
|
+ let remainder = suffix(count % chunksize)
|
|
|
+ if !remainder.isEmpty {
|
|
|
+ words.append(Array(remainder))
|
|
|
+ }
|
|
|
+ return words
|
|
|
+ }
|
|
|
+
|
|
|
+ public func md5() -> [Element] {
|
|
|
+ return Digest.md5(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func sha1() -> [Element] {
|
|
|
+ return Digest.sha1(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func sha224() -> [Element] {
|
|
|
+ return Digest.sha224(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func sha256() -> [Element] {
|
|
|
+ return Digest.sha256(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func sha384() -> [Element] {
|
|
|
+ return Digest.sha384(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func sha512() -> [Element] {
|
|
|
+ return Digest.sha512(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func sha2(_ variant: SHA2.Variant) -> [Element] {
|
|
|
+ return Digest.sha2(self, variant: variant)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func sha3(_ variant: SHA3.Variant) -> [Element] {
|
|
|
+ return Digest.sha3(self, variant: variant)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
|
|
|
+ return Checksum.crc32(self, seed: seed, reflect: reflect)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func crc16(seed: UInt16? = nil) -> UInt16 {
|
|
|
+ return Checksum.crc16(self, seed: seed)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func encrypt(cipher: Cipher) throws -> [Element] {
|
|
|
+ return try cipher.encrypt(slice)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func decrypt(cipher: Cipher) throws -> [Element] {
|
|
|
+ return try cipher.decrypt(slice)
|
|
|
+ }
|
|
|
+
|
|
|
+ public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Element] {
|
|
|
+ return try authenticator.authenticate(self)
|
|
|
+ }
|
|
|
}
|