Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
vwiencek committed Sep 11, 2014
1 parent b79a96a commit dbf4cfe
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 7 deletions.
11 changes: 11 additions & 0 deletions syncany-cli/src/main/java/org/syncany/Syncany.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@
*/
public class Syncany {
public static void main(String[] args) throws Exception {
/**
String link = "syncany://storage/1/AA-BB";
Pattern p = Pattern.compile("^syncany://storage/1/(?:(not-encrypted/)(.+)|([^-]+-(.+)))$");
Matcher linkMatcher = p.matcher(link);
for (int i = 0 ; i < linkMatcher.groupCount() ; i ++){
System.out.println(linkMatcher.group(i));
}
**/

String[] dd= new String[]{"--localdir=/Users/vwiencek/sandbox/client2", "connect", "syncany://storage/1/5sx8ke2sxTU58fmZbKRn7pqxWCdjDbg7qwExvVnSi4muqL9q9onwSj5qBwpgcwrE87JPmZLEQaD2YYXWZziU5oLT/BwuzZQ9n7dyKY4rZhS1dRfpCpkhGh3wBYiuoM6WZapT7vZ3JK8bvMExaqaZ2UAoLC72RanuccNNG878DrbDC7DSYADdQ9hk6qLoK3UiG7dQQaJWPipTTaNDfEE3d7FV3ts8r1UEAXNY8Yd1ax6FsZo9sxpmLSs63voz35qx1z6PKBaNbdMongH6gGcZjGHUaikMWv1atZmfmfyTsbm2FbcAp4eUuDwBfFVanNC3137ut61gsDc5oeeQ59Ey84oPaCxvcs2fxKfLPMveT4tSuNjfevf9XKqKw8mssWnwKmevsszTgfcjsESVZae2aiG9Tr4NyyJnCuXQTEf6twii"};
System.exit(new CommandLineClient(args).start());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.syncany.crypto.SaltedSecretKey;
import org.syncany.operations.Operation;
import org.syncany.plugins.UserInteractionListener;
import org.syncany.util.Base58;
import org.syncany.util.EnvironmentUtil;
import org.syncany.util.FileUtil;

Expand Down Expand Up @@ -148,12 +149,12 @@ protected String getEncryptedLink(ConnectionTO connectionTO, List<CipherSpec> ci
serializer.write(connectionTO, plaintextOutputStream);

byte[] masterKeySalt = masterKey.getSalt();
String masterKeySaltEncodedStr = new String(Base64.encodeBase64(masterKeySalt, false));
String masterKeySaltEncodedStr = new String(Base58.encode(masterKeySalt));

byte[] encryptedConnectionBytes = CipherUtil.encrypt(new ByteArrayInputStream(plaintextOutputStream.toByteArray()), cipherSuites, masterKey);
String encryptedEncodedStorageXml = new String(Base64.encodeBase64(encryptedConnectionBytes, false));
String encryptedEncodedStorageXml = new String(Base58.encode(encryptedConnectionBytes));

return "syncany://storage/1/" + masterKeySaltEncodedStr + "-" + encryptedEncodedStorageXml;
return "syncany://storage/1/" + masterKeySaltEncodedStr + "/" + encryptedEncodedStorageXml;
}

protected String getPlaintextLink(ConnectionTO connectionTO) throws Exception {
Expand All @@ -162,7 +163,7 @@ protected String getPlaintextLink(ConnectionTO connectionTO) throws Exception {
serializer.write(connectionTO, plaintextOutputStream);

byte[] plaintextStorageXml = plaintextOutputStream.toByteArray();
String plaintextEncodedStorageXml = new String(Base64.encodeBase64(plaintextStorageXml, false));
String plaintextEncodedStorageXml = new String(Base58.encode(plaintextStorageXml));

return "syncany://storage/1/not-encrypted/" + plaintextEncodedStorageXml;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.syncany.plugins.transfer.files.MasterRemoteFile;
import org.syncany.plugins.transfer.files.RemoteFile;
import org.syncany.plugins.transfer.files.RepoRemoteFile;
import org.syncany.util.Base58;

/**
* The connect operation connects to an existing repository at a given remote storage
Expand All @@ -69,7 +70,7 @@
public class ConnectOperation extends AbstractInitOperation {
private static final Logger logger = Logger.getLogger(ConnectOperation.class.getSimpleName());

private static final Pattern LINK_PATTERN = Pattern.compile("^syncany://storage/1/(?:(not-encrypted/)(.+)|([^-]+-(.+)))$");
private static final Pattern LINK_PATTERN = Pattern.compile("^syncany://storage/1/(?:(not-encrypted/)(.+)|([^-]+/(.+)))$");
private static final int LINK_PATTERN_GROUP_NOT_ENCRYPTED_FLAG = 1;
private static final int LINK_PATTERN_GROUP_NOT_ENCRYPTED_ENCODED = 2;
private static final int LINK_PATTERN_GROUP_ENCRYPTED_MASTER_KEY_SALT = 3;
Expand Down Expand Up @@ -258,8 +259,8 @@ private ConfigTO createConfigTOFromLink(ConfigTO configTO, String link) throws S
String masterKeySaltStr = linkMatcher.group(LINK_PATTERN_GROUP_ENCRYPTED_MASTER_KEY_SALT);
String ciphertext = linkMatcher.group(LINK_PATTERN_GROUP_ENCRYPTED_ENCODED);

byte[] masterKeySalt = Base64.decodeBase64(masterKeySaltStr);
byte[] ciphertextBytes = Base64.decodeBase64(ciphertext);
byte[] masterKeySalt = Base58.decode(masterKeySaltStr.split("/")[1]);
byte[] ciphertextBytes = Base58.decode(ciphertext);

boolean retryPassword = true;

Expand Down
117 changes: 117 additions & 0 deletions syncany-util/src/main/java/org/syncany/util/Base58.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.util;

import java.math.BigInteger;

/**
* base58 encoding
* @author Vincent Wiencek <vwiencek@gmail.com>
*/
public class Base58 {

private static final String Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
private static final BigInteger Base58 = new BigInteger("58", 10);

public static String encode(byte[] indata) {
byte[] data = new byte[indata.length];
System.arraycopy(indata, 0, data, 0, data.length);
// convert big endian data to little endian
int len = data.length;
int mid = len / 2;
int i;
for (i = 0; i < mid; i++) {
byte tmp = data[i];
data[i] = data[len - 1 - i];
data[len - 1 - i] = tmp;
}
BigInteger value = new BigInteger(1, data);
StringBuilder result = new StringBuilder();

// divide until zero
while (value.compareTo(BigInteger.ZERO) > 0) {
BigInteger[] qr = value.divideAndRemainder(Base58);
value = qr[0];
BigInteger rem = qr[1];
result.append(Alphabet.charAt(rem.intValue()));
}
// append leading zeros
for (i = 0; i < len; i++) {
if (data[i] == 0) {
result.append(Alphabet.charAt(0));
}
else {
break;
}
}
// reverse to big endian
len = result.length();
mid = len / 2;
for (i = 0; i < mid; i++) {
char ch1 = result.charAt(i);
char ch2 = result.charAt(len - 1 - i);
result.setCharAt(len - 1 - i, ch1);
result.setCharAt(i, ch2);
}
return result.toString();
}

public static byte[] decode(String data) {
BigInteger value = BigInteger.ZERO;
BigInteger character;
// eat spaces
data = data.trim();
// encode to big number
int i;
int len = data.length();
for (i = 0; i < len; i++) {
int pos = Alphabet.indexOf(data.charAt(i));
if (pos < 0) { // not a valid char
return null;
}
character = new BigInteger(String.valueOf(pos), 10);
value = value.multiply(Base58);
value = value.add(character);
}

byte[] dec = value.toByteArray();
int declen = dec.length;
int decoff = 0;
// strip sign byte
if (dec.length > 2 && dec[0] == 0 && (dec[1] & 0x80) == 0x80) {
declen--;
decoff++;
}
// count leading zeros
int n = 0;
for (i = 0; i < len; i++) {
if (data.charAt(i) == Alphabet.charAt(0)) {
n++;
}
else {
break;
}
}
// reverse result at the right place
byte[] result = new byte[declen + n];
for (i = 0; i < declen; i++) {
result[i] = dec[(declen - 1 - i) + decoff];
}
return result;
}
}
36 changes: 36 additions & 0 deletions syncany-util/src/test/java/org/syncany/tests/util/Base58Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.tests.util;

import static org.junit.Assert.assertTrue;

import java.util.Arrays;

import org.junit.Test;
import org.syncany.util.Base58;

public class Base58Test {

@Test
public void testGetRelativePath() {
byte[] testString = "life is easy with eyes closed".getBytes();
byte[] decodedString = Base58.decode(Base58.encode(testString));

assertTrue(Arrays.equals(testString, decodedString));
}
}

0 comments on commit dbf4cfe

Please sign in to comment.