/
PKCS7.swift
41 lines (36 loc) · 1.18 KB
/
PKCS7.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// PKCS7.swift
// CryptoSwift
//
// Created by Marcin Krzyzanowski on 27/12/14.
// Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
//
import Foundation
public struct PKCS7 {
let data:NSData
public init(data:NSData) {
self.data = data;
}
public func addPadding(blockSizeBytes:UInt8) -> NSData {
var padding = blockSizeBytes - (data.length % Int(blockSizeBytes))
var withPadding = NSMutableData(data: data)
if (padding == 0) {
// If the original data is a multiple of N bytes, then an extra block of bytes with value N is added.
for i in 0..<blockSizeBytes {
withPadding.appendBytes([blockSizeBytes])
}
} else {
// The value of each added byte is the number of bytes that are added
for i in 0..<padding {
withPadding.appendBytes([padding])
}
}
return withPadding
}
public func removePadding() -> NSData {
if let padding = data.bytes().last {
return data.subdataWithRange(NSRange(location: 0, length: data.length - Int(padding)))
}
return data
}
}