/**
 * Original iOS version by  Jens Alfke
 * Ported to Android by Marty Schoch
 *
 * Copyright (c) 2012 Couchbase, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

package com.couchbase.cblite;

import java.util.Arrays;

/**
 * Key identifying a data blob. This happens to be a SHA-1 digest.
 */
public class CBLBlobKey {

    private byte[] bytes;

    public CBLBlobKey() {
    }

    public CBLBlobKey(byte[] bytes) {
        this.bytes = bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public byte[] getBytes() {
        return bytes;
    };

    public static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        }
        return buf.toString();
    }

    public static byte[] convertFromHex(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }

    @Override
    public boolean equals(Object o) {
        if(!(o instanceof CBLBlobKey)) {
            return false;
        }
        CBLBlobKey oBlobKey = (CBLBlobKey)o;
        return Arrays.equals(getBytes(), oBlobKey.getBytes());
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(bytes);
    }


    @Override
    public String toString() {
        return CBLBlobKey.convertToHex(bytes);
    }
}

@@ -719,16 +719,90 @@
 blite;%0A%0A
+%0Aimport com.couchbase.cblite.support.Base64;%0A%0Aimport java.io.IOException;%0A
 import j
@@ -1046,32 +1046,1169 @@
 = bytes;%0A    %7D%0A%0A
+    /**%0A     * Constructor%0A     *%0A     * @param base64Digest string with base64'd digest, with leading %22sha1-%22 attached.%0A     *                     eg, %22sha1-LKJ32423JK...%22%0A     */%0A    public CBLBlobKey(String base64Digest) %7B%0A        this(decodeBase64Digest(base64Digest));%0A    %7D%0A%0A    /**%0A     * Decode base64'd digest into a byte array that is suitable for use%0A     * as a blob key.%0A     *%0A     * @param base64Digest string with base64'd digest, with leading %22sha1-%22 attached.%0A     *                     eg, %22sha1-LKJ32423JK...%22%0A     * @return a byte%5B%5D blob key%0A     */%0A    private static byte%5B%5D decodeBase64Digest(String base64Digest) %7B%0A        String expectedPrefix = %22sha1-%22;%0A        if (!base64Digest.startsWith(expectedPrefix)) %7B%0A            throw new IllegalArgumentException(base64Digest + %22 did not start with %22 + expectedPrefix);%0A        %7D%0A        base64Digest = base64Digest.replaceFirst(expectedPrefix, %22%22);%0A        byte%5B%5D bytes = new byte%5B0%5D;%0A        try %7B%0A            bytes = Base64.decode(base64Digest);%0A        %7D catch (IOException e) %7B%0A            new IllegalArgumentException(e);%0A        %7D%0A        return bytes;%0A    %7D%0A%0A
     public void 

add a way to lookup blob by base64 digest. issue #47