Passed
Push — master ( 59bb35...8722e3 )
by Vincent
04:28
created

serialize(CellData[])   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
/*
2
 * This file is part of ArakneUtils.
3
 *
4
 * ArakneUtils is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * ArakneUtils is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with ArakneUtils.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2020 Vincent Quatrevieux
18
 */
19
20
package fr.arakne.utils.maps.serializer;
21
22
import fr.arakne.utils.encoding.Checksum;
23
import fr.arakne.utils.encoding.Key;
24
25
/**
26
 * Implementation of the map serializer for encrypted map data
27
 * Encrypted maps are maps with file name ending with X.swf (ex: 10340_0706131721X.swf)
28
 * and require a key sent by the server with the GDM packet
29
 *
30
 * <code>
31
 *     MapDataSerializer serializer = new EncryptedMapDataSerializer(Key.parse(gdm.key()));
32
 *     CellData[] cells = serializer.deserialize(encryptedMapData);
33
 * </code>
34
 *
35
 * https://github.com/Emudofus/Dofus/blob/1.29/dofus/managers/MapsServersManager.as#L137
36
 */
37
final public class EncryptedMapDataSerializer implements MapDataSerializer {
38
    final private Key key;
39
    final private MapDataSerializer plainDataSerializer;
40
41
    public EncryptedMapDataSerializer(Key key) {
42 1
        this(key, new DefaultMapDataSerializer());
43 1
    }
44
45 1
    public EncryptedMapDataSerializer(Key key, MapDataSerializer plainDataSerializer) {
46 1
        this.key = key;
47 1
        this.plainDataSerializer = plainDataSerializer;
48 1
    }
49
50
    @Override
51
    public CellData[] deserialize(String mapData) {
52 1
        return plainDataSerializer.deserialize(key.cipher().decrypt(mapData, Checksum.integer(key.toString()) * 2));
53
    }
54
55
    @Override
56
    public String serialize(CellData[] cells) {
57 1
        return key.cipher().encrypt(plainDataSerializer.serialize(cells), Checksum.integer(key.toString()) * 2);
58
    }
59
}
60