public static RevisionID RevIDForJSON(IEnumerable <byte> json, bool deleted, RevisionID prevRevID)
        {
            // Revision IDs have a generation count, a hyphen, and a hex digest
            var generation = 0;

            if (prevRevID != null)
            {
                generation = prevRevID.Generation;
                if (generation == 0)
                {
                    return(null);
                }
            }

            // Generate a digest for this revision based on the previous revision ID, document JSON,
            // and attachment digests. This doesn't need to be secure; we just need to ensure that this
            // code consistently generates the same ID given equivalent revisions.
            MessageDigest md5Digest;

            try {
                md5Digest = MessageDigest.GetInstance("MD5");
            } catch (NotSupportedException) {
                throw Misc.CreateExceptionAndLog(Log.To.Database, Tag, "Failed to acquire a class to create MD5");
            }

            if (prevRevID != null)
            {
                var prevIDData = prevRevID.AsData();
                var length     = prevIDData.Length;
                var lengthByte = unchecked ((byte)(length & unchecked ((0xFF))));
                md5Digest.Update(lengthByte);
                if (lengthByte > 0)
                {
                    md5Digest.Update(prevIDData);
                }
            }

            var isDeleted = deleted ? 1 : 0;

            md5Digest.Update((byte)isDeleted);

            if (json != null)
            {
                md5Digest.Update(json.ToArray());
            }

            var md5DigestResult       = md5Digest.Digest();
            var digestAsHex           = BitConverter.ToString(md5DigestResult).Replace("-", String.Empty);
            int generationIncremented = generation + 1;

            return(RevisionIDFactory.FromString(String.Format("{0}-{1}", generationIncremented, digestAsHex).ToLower()));
        }
Example #2
0
        public static IEnumerable <RevisionID> AsMaybeRevIDs(this IList list)
        {
            foreach (var obj in list)
            {
                var str = obj as string;
                if (str == null)
                {
                    yield return(null);
                }

                yield return(RevisionIDFactory.FromString(str));
            }
        }
Example #3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var str = reader.Value as string;

            return(RevisionIDFactory.FromString(str));
        }
Example #4
0
 public static IEnumerable <RevisionID> AsRevIDs(this IList <string> list)
 {
     return(list?.Select(x => RevisionIDFactory.FromString(x)));
 }
Example #5
0
 public static RevisionID AsRevID(this string str)
 {
     return(RevisionIDFactory.FromString(str));
 }