-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.swift
73 lines (64 loc) · 1.88 KB
/
lib.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*********************************
Use: "Hello World!".encrypt("key")
"Hello World!".encrypt("key").decrypt("key")
*********************************/
import Foundation
// Get array of ascii values from string
extension String {
var asciiArray: [UInt32] {
return unicodeScalars.filter{$0.isASCII}.map{$0.value}
}
}
extension Character {
var asciiValue: UInt32? {
return String(self).unicodeScalars.filter{$0.isASCII}.first?.value
}
}
// Base 64
extension String {
func fromBase64() -> String? {
guard let data = Data(base64Encoded: self) else {
return nil
}
return String(data: data, encoding: .utf8)
}
func toBase64() -> String {
return Data(self.utf8).base64EncodedString()
}
}
extension String {
func encrypt(_ keyValue: String) -> String {
let encoded = self.asciiArray
var keyEncoded = keyValue.asciiArray
let array = encoded.map({
x -> (UInt32) in
var y = x
for i in keyEncoded {
y = (y + 1) << (i % 8)
}
keyEncoded = keyEncoded.reversed()
return y;
})
return array.description.toBase64()
}
func decrypt(_ keyValue: String) -> String {
let toString = self.fromBase64()
let chars = CharacterSet(charactersIn: ",][ ")
let encoded = toString!.components(separatedBy: chars).filter {$0 != ""}.flatMap { UInt32($0)}
var keyEncoded = keyValue.asciiArray
let array = encoded.map({
x -> (UInt32) in
keyEncoded = keyEncoded.reversed()
var y = x
for i in keyEncoded {
y = (y - 1) >> (i % 8)
}
return y;
})
var output = "";
for i in array {
output += String(UnicodeScalar(UInt8(i)))
}
return output
}
}