Forum rules
Under no circumstances is spamming or advertising of any kind allowed. Do not post any abusive, obscene, vulgar, slanderous, hateful, threatening, sexually-orientated or any other material that may violate others security. Profanity or any kind of insolent behavior to other members (regardless of rank) will not be tolerated. Remember, what you don’t find offensive can be offensive to other members. Please treat each other with the kind of reverence you’d expect from other members.
Failure to comply with any of the above will result in users being banned without notice. If any further details are needed, contact: “The team” using the link at the bottom of the forum page. Thank you.
User avatar
eduo
Posts: 716
Joined: Sat Feb 10, 2007 1:40 am
Location: Information Technology
Contact: ICQ Website Yahoo Messenger

Hash code for Swift 2

Mon Jun 15, 2015 5:04 pm

Hi.

The trac is no longer editable by everyone. I suggest this is put as the source code for a simple OSHash class in Swift 2 (especially interesting now that Swift exists not only for iOS and OS X but also is open source and works on Linux):

http://trac.opensubtitles.org/projects/ ... ourceCodes

Code: Select all

// OSHash.swift // Originally implemented from Objective-C version for Swift by omerucel 18/04/2015 // http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes#Objective-C // Updated for Swift 2 by eduo on 15/06/15. // Copyright © 2015 Eduardo Gutierrez. All rights reserved. // import Foundation class OSHashAlgorithm: NSObject { let chunkSize: Int = 65536; struct VideoHash { var fileHash: String var fileSize: UInt64 } func hashForPath (path: String) -> VideoHash { var fileHash = VideoHash(fileHash: "", fileSize: 0) let fileHandler = NSFileHandle(forReadingAtPath: path)! let fileDataBegin: NSData = fileHandler.readDataOfLength(chunkSize) fileHandler.seekToEndOfFile() let fileSize: UInt64 = fileHandler.offsetInFile if (UInt64(chunkSize) > fileSize) { return fileHash } fileHandler.seekToFileOffset(max(0, fileSize - UInt64(chunkSize))) let fileDataEnd: NSData = fileHandler.readDataOfLength(chunkSize) var hash: UInt64 = fileSize var data_bytes = UnsafeBufferPointer<UInt64>( start: UnsafePointer(fileDataBegin.bytes), count: fileDataBegin.length/sizeof(UInt64) ) hash = data_bytes.reduce(hash,combine: &+) data_bytes = UnsafeBufferPointer<UInt64>( start: UnsafePointer(fileDataEnd.bytes), count: fileDataEnd.length/sizeof(UInt64) ) hash = data_bytes.reduce(hash,combine: &+) fileHash.fileHash = String(format:"%qx", arguments: [hash]) fileHash.fileSize = fileSize fileHandler.closeFile() return fileHash } }
This can then be called from anywhere by calling on the class:
var osha = OSHashAlgorithm()
var result = osha.hashForPath(fileName)
println(result.fileHash)
println(result.fileSize)
http://eduo.info/
[url=http://eduo.info/soleol/]OpenSubtitles from your desktop: SolEol for Mac/Windows/Linux[/url]
[url=http://forums.plexapp.com/index.php?showtopic=325&st=0&p=2480&#entry2480]My current episode processing work flow[/url].

User avatar
oss
Site Admin
Posts: 5879
Joined: Sat Feb 25, 2006 11:26 pm
Contact: Website

Re: Hash code for Swift 2

Wed Jun 17, 2015 8:30 am

yep, it was flooded with spam, so...

anyway, it is updated:

http://trac.opensubtitles.org/projects/ ... des#SWIFT2

thanks for it, for sure it will helps others.

Return to “Developing”

Who is online

Users browsing this forum: No registered users and 26 guests