qid
int64
1
74.7M
question
stringlengths
0
58.3k
date
stringlengths
10
10
metadata
sequence
response_j
stringlengths
2
48.3k
response_k
stringlengths
2
40.5k
90,984
I'm about to start a campaign using MgT 2e and I have tons of GT material but I've read over and over again how **Behind the Claw** (for GT) differs from **OTU**. I'm not interested on moving rules from GURPS to MgT 2e, but information, so I just want to know which are the points where **GT** background is different from **OTU** (beyond the Dulinor issues). Bear in mind that I intend to play the *Golden Age* (1105-) or just after the *Fifth Frontier War*.
2016/11/29
[ "https://rpg.stackexchange.com/questions/90984", "https://rpg.stackexchange.com", "https://rpg.stackexchange.com/users/7885/" ]
What I found in sea or space battles is that players have no clue what ships look like. No matter how popular you think the ships are, some of your players will not be able to visualize them in the detail you expect. Take for example a [three master](https://www.google.de/search?q=three+master&tbm=isch&tbo=u&source=univ&sa=X&ved=0ahUKEwi4o6uVoM7QAhXkDZoKHWPRCZ8QsAQILA&biw=1433&bih=760). If the DM says something like "after the smoke of your broadside clears, you see the enemy ship has sustained heavy damage, it's foremast broken and hanging over the side", expect players to not be able to accurately describe the scene, no matter how many popular movies you have seen and you *think* they have seen. And it gets even worse with space battles, with all kind of popular fiction depicting ships differently. Make sure you have pictures or props of the ships or ideally, both. Being able to picture the scene, even if the pictures are given, is a huge boost in playability. Prepare in advance as a DM, so you have pictures of possible encounters, possible damage and other outcomes (beaches, asteroids, whatever). Rogue Trader and 40K for example has a very distinct style and that style is a big part of how it *feels*. Fighting an Ork dreadnought or a sleek Eldar frigate *feels* completely different even if you are rolling the same dice. There's pictures of both all over the net. And if you want to have a great evening, maybe you can find some Battlefleet Gothic miniatures to put in the middle of the table. Now if you and your players are unhappy with the *system* I guess you need to find a better system. Personally, I was happy with Rogue Trader, but if you are not, maybe it's just not the right system for you.
My answer is for my experience with space battles not sea. Space battles in most games I have run tend to have terrible rules that bear little relation to how things move in space or the vast distances and ridiculous speeds. Space battles seem to me to be games of chess, with ships dancing around each other trying to get a line of fire on the enemy while avoiding having the same occur to them. As the point that a large lance weapon or broadside can be brought to bear the game is often over. Boarding in space is more fun as you have to match velocity to dock/storm the vessel or have some kind of teleporter or boarding shuttles. To keep this fluid I tend to bin most movement rules(most of the rules) in the system. And take speed as the change in velocity a ship can affect. Also rotating on the spot in space takes virtually no effort, but changing direction should be difficult. I take acceleration/speed to be in mm so a speed of 6 is 6mm/turn/turn on the table, as after a few turns of combat you can be covering large distances. So if travelling in a straight line you cover 6mm in first turn 12mm in second, 18 in third etc. So you can go very "fast" but stopping or turning becomes more difficult the faster you go. I clear as large a table as possible. Battle start far apart, space is big and I like this to be represented. I also give players 60 seconds to plan their next move; so if flows quickly. If they don't give me written instructions at the 60s mark then they continue to do what they did last turn. Think of it like a bridge on a ship where the command staff discuss tactics and plan and then pass notes to the helmsman and gunnery staff. I also have to have written down what I am going to do with the NPC ships before I look at their notes. Often for enemy ships I have a plan and contingencies and follow them so I as GM can't be accused of cheating. I find the dancing is the fun, there are very few dice roles unless you want to overdirve the engines or jury rig your ship in some silly way. The game flows as the ships close or circle each other. The roles are only needed for targeting, firing and damage. I don't know if this is what you were looking for but my players really enjoyed their space battles when run this way. Try it, see what works and what doesn't for you and tweak as needed.
90,984
I'm about to start a campaign using MgT 2e and I have tons of GT material but I've read over and over again how **Behind the Claw** (for GT) differs from **OTU**. I'm not interested on moving rules from GURPS to MgT 2e, but information, so I just want to know which are the points where **GT** background is different from **OTU** (beyond the Dulinor issues). Bear in mind that I intend to play the *Golden Age* (1105-) or just after the *Fifth Frontier War*.
2016/11/29
[ "https://rpg.stackexchange.com/questions/90984", "https://rpg.stackexchange.com", "https://rpg.stackexchange.com/users/7885/" ]
As a Rogue Trader RPG veteran, I understand your problem. RT is trying to combine two distinct gaming experiences; RPG’s and wargaming. In wargaming, the pleasure is derived from fighting the battles according to the rules with the players commanding either units or parts of the battlefield. In RPG’s, the players play their characters. It’s the combination that fails IMO. Our solution was to throw away the minutiae of ship fighting and make it a more of a RPG session. So instead of mapping out the manoeuvres, broadsides etc., change it into a story in which the players have certain parts to play. And use the crew’s rating, not the players for the ship’s rolls. Players can give a bonus but it makes no sense that for example a single player’s gunnery rating works for a ship of 100,000 crew…. So describe noticing the enemy ship on the scanners (perhaps insert a scene where the scanners go on the fritz and the tech-priest has to fix it), manoeuvring the ship into an advantageous combat position (insert scene where a rudder fails, the engines go off-line, zombies come crawling out of the ventilation ducts, the crew needs a hearty speech etc.), then describe the first salvoes and roll for damage. Describe the effect of damage on your ship and then let the players effect repairs or work around the issues. In short, eliminate tactical rounds, just play the enemy ship intelligently, and throw in scenes/challenges for the players aboard their own ship. Space combat takes forever so there is enough time to solve (small) issues before the next broadside arrives. The entire encounter should be tense and nerve-wracking and things are always breaking down at the worst moment. The players should be running around fixing things or getting the crew to work at maximum efficiency. That also offers them the chance to use their skills instead of just moving the ship and firing every round.
My answer is for my experience with space battles not sea. Space battles in most games I have run tend to have terrible rules that bear little relation to how things move in space or the vast distances and ridiculous speeds. Space battles seem to me to be games of chess, with ships dancing around each other trying to get a line of fire on the enemy while avoiding having the same occur to them. As the point that a large lance weapon or broadside can be brought to bear the game is often over. Boarding in space is more fun as you have to match velocity to dock/storm the vessel or have some kind of teleporter or boarding shuttles. To keep this fluid I tend to bin most movement rules(most of the rules) in the system. And take speed as the change in velocity a ship can affect. Also rotating on the spot in space takes virtually no effort, but changing direction should be difficult. I take acceleration/speed to be in mm so a speed of 6 is 6mm/turn/turn on the table, as after a few turns of combat you can be covering large distances. So if travelling in a straight line you cover 6mm in first turn 12mm in second, 18 in third etc. So you can go very "fast" but stopping or turning becomes more difficult the faster you go. I clear as large a table as possible. Battle start far apart, space is big and I like this to be represented. I also give players 60 seconds to plan their next move; so if flows quickly. If they don't give me written instructions at the 60s mark then they continue to do what they did last turn. Think of it like a bridge on a ship where the command staff discuss tactics and plan and then pass notes to the helmsman and gunnery staff. I also have to have written down what I am going to do with the NPC ships before I look at their notes. Often for enemy ships I have a plan and contingencies and follow them so I as GM can't be accused of cheating. I find the dancing is the fun, there are very few dice roles unless you want to overdirve the engines or jury rig your ship in some silly way. The game flows as the ships close or circle each other. The roles are only needed for targeting, firing and damage. I don't know if this is what you were looking for but my players really enjoyed their space battles when run this way. Try it, see what works and what doesn't for you and tweak as needed.
90,984
I'm about to start a campaign using MgT 2e and I have tons of GT material but I've read over and over again how **Behind the Claw** (for GT) differs from **OTU**. I'm not interested on moving rules from GURPS to MgT 2e, but information, so I just want to know which are the points where **GT** background is different from **OTU** (beyond the Dulinor issues). Bear in mind that I intend to play the *Golden Age* (1105-) or just after the *Fifth Frontier War*.
2016/11/29
[ "https://rpg.stackexchange.com/questions/90984", "https://rpg.stackexchange.com", "https://rpg.stackexchange.com/users/7885/" ]
As a Rogue Trader RPG veteran, I understand your problem. RT is trying to combine two distinct gaming experiences; RPG’s and wargaming. In wargaming, the pleasure is derived from fighting the battles according to the rules with the players commanding either units or parts of the battlefield. In RPG’s, the players play their characters. It’s the combination that fails IMO. Our solution was to throw away the minutiae of ship fighting and make it a more of a RPG session. So instead of mapping out the manoeuvres, broadsides etc., change it into a story in which the players have certain parts to play. And use the crew’s rating, not the players for the ship’s rolls. Players can give a bonus but it makes no sense that for example a single player’s gunnery rating works for a ship of 100,000 crew…. So describe noticing the enemy ship on the scanners (perhaps insert a scene where the scanners go on the fritz and the tech-priest has to fix it), manoeuvring the ship into an advantageous combat position (insert scene where a rudder fails, the engines go off-line, zombies come crawling out of the ventilation ducts, the crew needs a hearty speech etc.), then describe the first salvoes and roll for damage. Describe the effect of damage on your ship and then let the players effect repairs or work around the issues. In short, eliminate tactical rounds, just play the enemy ship intelligently, and throw in scenes/challenges for the players aboard their own ship. Space combat takes forever so there is enough time to solve (small) issues before the next broadside arrives. The entire encounter should be tense and nerve-wracking and things are always breaking down at the worst moment. The players should be running around fixing things or getting the crew to work at maximum efficiency. That also offers them the chance to use their skills instead of just moving the ship and firing every round.
What I found in sea or space battles is that players have no clue what ships look like. No matter how popular you think the ships are, some of your players will not be able to visualize them in the detail you expect. Take for example a [three master](https://www.google.de/search?q=three+master&tbm=isch&tbo=u&source=univ&sa=X&ved=0ahUKEwi4o6uVoM7QAhXkDZoKHWPRCZ8QsAQILA&biw=1433&bih=760). If the DM says something like "after the smoke of your broadside clears, you see the enemy ship has sustained heavy damage, it's foremast broken and hanging over the side", expect players to not be able to accurately describe the scene, no matter how many popular movies you have seen and you *think* they have seen. And it gets even worse with space battles, with all kind of popular fiction depicting ships differently. Make sure you have pictures or props of the ships or ideally, both. Being able to picture the scene, even if the pictures are given, is a huge boost in playability. Prepare in advance as a DM, so you have pictures of possible encounters, possible damage and other outcomes (beaches, asteroids, whatever). Rogue Trader and 40K for example has a very distinct style and that style is a big part of how it *feels*. Fighting an Ork dreadnought or a sleek Eldar frigate *feels* completely different even if you are rolling the same dice. There's pictures of both all over the net. And if you want to have a great evening, maybe you can find some Battlefleet Gothic miniatures to put in the middle of the table. Now if you and your players are unhappy with the *system* I guess you need to find a better system. Personally, I was happy with Rogue Trader, but if you are not, maybe it's just not the right system for you.
7,260,278
Good day, i was having a little trouble with the following code below and i got a solution from a ticked answer in this link. I would like to know why. Note: i had a similar type of layout as in the question, but with one TextView only. [solution link here](https://stackoverflow.com/questions/6054773/classcastexception-in-my-application) my code below: ``` public void onItemClick(AdapterView<?> av, View v, int position, long id) { blueadapter.cancelDiscovery(); String info = ((TextView) v).getText().toString(); //classcastException here String Bluetooth_address = info.substring(info.length()-17); } ``` so from his solution, if i change this to this ``` String info = ((TextView) v.findViewById(R.id.search_device_id)).getText().toString(); ``` it solves my ClassCastException problem. Now please can someone kindly explain to me or point me to the right direction, what he meant by in the answer **"You receive the whole LinearLayout as the parameter v. You should try v.findViewById() and then use this textview."** its nice it solves my problem, but i would like to understand why i had to do that?.. i have dealt with ListViews before, but i have not come across this, so its kind of strange for me. Thank you.
2011/08/31
[ "https://Stackoverflow.com/questions/7260278", "https://Stackoverflow.com", "https://Stackoverflow.com/users/316843/" ]
According to the exception, you're calling this on a string, which doesn't know that method. You need to call it on wherever your Barby::Barcode instance is. According to the documentation "Bookland" is a special case of "pure" EAN-13 data. So, maybe doing something like ``` bc = Barby::EAN13.new(productidentifier_idvalue) bc.valid? ``` Should get you closer to your aim
The valid? method exists only on objects which are instances of the [Barcode class](http://barby.rubyforge.org/rdoc/classes/Barby/Barcode.html) You're calling this method on a String.
7,260,278
Good day, i was having a little trouble with the following code below and i got a solution from a ticked answer in this link. I would like to know why. Note: i had a similar type of layout as in the question, but with one TextView only. [solution link here](https://stackoverflow.com/questions/6054773/classcastexception-in-my-application) my code below: ``` public void onItemClick(AdapterView<?> av, View v, int position, long id) { blueadapter.cancelDiscovery(); String info = ((TextView) v).getText().toString(); //classcastException here String Bluetooth_address = info.substring(info.length()-17); } ``` so from his solution, if i change this to this ``` String info = ((TextView) v.findViewById(R.id.search_device_id)).getText().toString(); ``` it solves my ClassCastException problem. Now please can someone kindly explain to me or point me to the right direction, what he meant by in the answer **"You receive the whole LinearLayout as the parameter v. You should try v.findViewById() and then use this textview."** its nice it solves my problem, but i would like to understand why i had to do that?.. i have dealt with ListViews before, but i have not come across this, so its kind of strange for me. Thank you.
2011/08/31
[ "https://Stackoverflow.com/questions/7260278", "https://Stackoverflow.com", "https://Stackoverflow.com/users/316843/" ]
If you're giving an invalid ISBN here ``` barcode = Barby::Bookland.new("'#{self.productidentifier_idvalue}'") ``` it's no wonder it gives you the `data not valid` error. Check the value of `self.productidentifier_idvalue` and make sure it's 12 digits (the regexp to validate the code can be seen [here](https://github.com/toretore/barby/blob/master/lib/barby/barcode/ean_13.rb#L53)). **Update** Replace `"'#{self.productidentifier_idvalue}'"` with `self.productidentifier_idvalue`. The way you're doing it, the result of your expression is `'123456789012'`, which is two quotes too many. Besides, you don't really need to call `valid?`, it's done in the initializer as you can see [here](https://github.com/toretore/barby/blob/master/lib/barby/barcode/bookland.rb#L15).
According to the exception, you're calling this on a string, which doesn't know that method. You need to call it on wherever your Barby::Barcode instance is. According to the documentation "Bookland" is a special case of "pure" EAN-13 data. So, maybe doing something like ``` bc = Barby::EAN13.new(productidentifier_idvalue) bc.valid? ``` Should get you closer to your aim
7,260,278
Good day, i was having a little trouble with the following code below and i got a solution from a ticked answer in this link. I would like to know why. Note: i had a similar type of layout as in the question, but with one TextView only. [solution link here](https://stackoverflow.com/questions/6054773/classcastexception-in-my-application) my code below: ``` public void onItemClick(AdapterView<?> av, View v, int position, long id) { blueadapter.cancelDiscovery(); String info = ((TextView) v).getText().toString(); //classcastException here String Bluetooth_address = info.substring(info.length()-17); } ``` so from his solution, if i change this to this ``` String info = ((TextView) v.findViewById(R.id.search_device_id)).getText().toString(); ``` it solves my ClassCastException problem. Now please can someone kindly explain to me or point me to the right direction, what he meant by in the answer **"You receive the whole LinearLayout as the parameter v. You should try v.findViewById() and then use this textview."** its nice it solves my problem, but i would like to understand why i had to do that?.. i have dealt with ListViews before, but i have not come across this, so its kind of strange for me. Thank you.
2011/08/31
[ "https://Stackoverflow.com/questions/7260278", "https://Stackoverflow.com", "https://Stackoverflow.com/users/316843/" ]
If you're giving an invalid ISBN here ``` barcode = Barby::Bookland.new("'#{self.productidentifier_idvalue}'") ``` it's no wonder it gives you the `data not valid` error. Check the value of `self.productidentifier_idvalue` and make sure it's 12 digits (the regexp to validate the code can be seen [here](https://github.com/toretore/barby/blob/master/lib/barby/barcode/ean_13.rb#L53)). **Update** Replace `"'#{self.productidentifier_idvalue}'"` with `self.productidentifier_idvalue`. The way you're doing it, the result of your expression is `'123456789012'`, which is two quotes too many. Besides, you don't really need to call `valid?`, it's done in the initializer as you can see [here](https://github.com/toretore/barby/blob/master/lib/barby/barcode/bookland.rb#L15).
The valid? method exists only on objects which are instances of the [Barcode class](http://barby.rubyforge.org/rdoc/classes/Barby/Barcode.html) You're calling this method on a String.
67,657,481
I have a sheet called [Calendar] with a calendar for each month of the year, this sheet has conditional formatting to colour each date depending on holidays/capacity for that month. These dates are also set in the [Calendar] sheet. I have a second sheet for each month (eg. [Jan 2022], this sheet has lots of information about what's happening that month. I'd like to have that months calendar from [Calendar] show up on [Jan 2022] keeping all of the conditional formatting. Is this possible? (I realise that I could re-do all of the conditional formatting on the Monthly sheets based on the info in [Calendar] using `Calendar!Range` in the conditions but there are 45 different sets of conditional formatting as well as a script which changes the border colour of certain cells if they match other conditions on the sheet. I was wondering if there was an easier way)
2021/05/23
[ "https://Stackoverflow.com/questions/67657481", "https://Stackoverflow.com", "https://Stackoverflow.com/users/15753969/" ]
There is current a feature request to set a ranges border color and style. If you want that sort of feature you may wish to go to this issue and star the request. [36756953](https://issuetracker.google.com/issues/36756953)
Ok, After a LOT of messing about, I've found a few possible solutions that worked. Firstly, you can copy/paste the area you'd like to 'show' which will also copy the formatting over, sadly this formatting doesn't link to the correct page anymore (if, as in my case, you're not also copying the information that the conditions rely on). The easiest way I found was to copy the fields that the conditions rely on into the same fields on the sheet you're copying to (eg. in the cell Jan!X10 put the code `=Calendar!X10` and do the same for X10:Z20 for example). This means that when [Calendar] is updated, the conditional formatting on [Jan] changes to represent this. [one last tip, after putting the code in the matching cells, you can then either hide these cells OR copy/paste these cells to somewhere that works better in your sheet and the references in the conditional formatting will update] The other way I found to do it is to change the [Jan] conditional formatting to reference the [Calendar] page, sadly, you cant simply change `=A1>X10` to `=A1>Calendar!X10`, you need to change it to `=A1>INDIRECT("'Calendar'!X10")` as you cant directly use a cross-sheet reference in Conditional Formatting (for some reason). I hope this helps anyone who is having the same problems I was having. Happy coding :)
6,660,172
I need to specify a Index hint for my HQL ``` Query.setComment() ``` method is of no use as it is appending the hint before the select clause, though this can be achieved by NativeSQL but I just wanted to check is there a way we can achieve this in HQL rather then SQL
2011/07/12
[ "https://Stackoverflow.com/questions/6660172", "https://Stackoverflow.com", "https://Stackoverflow.com/users/537445/" ]
Well, skype uses propriatery protocols, and there are some efforts of reversing them: <http://en.wikipedia.org/wiki/Skype_protocol>
**Update** It is now possible to connect through “Skype for Web”, allowing you to just use your browser to connect to the (clunky) web client. Way better: [a recent plugin for Pidgin](https://github.com/EionRobb/skype4pidgin/tree/master/skypeweb) lets you use it directly through Pidgin, effectively adding true support for Skype in Pidgin.
16,590,765
I am looking to create a **mardown directive** (restrict A) which would make me able to use same recipient for **ng-view**. So I would basically load only .md files in views and apply my function on its content each time ng-view change. So : **index.html** ``` <div markdown ng-view></div> ``` with two views containing, let say, **view1.md** ``` #That should be h1 ``` and **view2.md** ``` ##That should be h2, no ? ``` My actual code is ``` 'use strict'; angular.module('btford.markdown', []). directive('markdown', function () { var converter = new Showdown.converter(); return { restrict: 'A', link: function (scope, element, attrs) { scope.$watch(element.html(), function(value) { var htmlText = converter.makeHtml(element.html()); element.html(htmlText); }); var htmlText = converter.makeHtml(element.text()); element.html(htmlText); } } }); ```
2013/05/16
[ "https://Stackoverflow.com/questions/16590765", "https://Stackoverflow.com", "https://Stackoverflow.com/users/2390493/" ]
The first param of watch can be a function, return any value you want including your $element.html(). You can even do a combination of data ``` $scope.$watch( function() { return $element.attr("abc") + $scope.variable + someinternalvar; }, function(newVal, oldVal) { doTheStuff(); } ); ``` Obviously the more intense the data you put in here the slower your watches will be. Use caution. -- FYI You should clean up your watchers, create an array and push the results of $scope.$watch into that array. Then on the $destroy message remove them. Also remember to unbind events as they will cause eventual performance issues as scopes are created & destroyed. ``` $document.bind('click', clickMe); $(window).on("resize", winResize); var watches = [] watches.push($scope.$watch("thing", function() { Thing(); })); $scope.$on("$destroy", function () { for (var i in watches) watches[i](); $document.unbind('click', clickMe); $(window).off("resize", winResize); }); ``` -- EDIT 2016-07-14 Just to add, cleaning up scope watchers is not needed as they are already processed internally, however rootScope, parent, etc. you should absolutely cleanup.
You can only watch variables on your scope. ``` scope.foo = 'bar'; scope.$watch('foo', function(newValue) { // Code to execute here }); ``` If you want to monitor the changes of DOM elements you need to do this on your own.
16,590,765
I am looking to create a **mardown directive** (restrict A) which would make me able to use same recipient for **ng-view**. So I would basically load only .md files in views and apply my function on its content each time ng-view change. So : **index.html** ``` <div markdown ng-view></div> ``` with two views containing, let say, **view1.md** ``` #That should be h1 ``` and **view2.md** ``` ##That should be h2, no ? ``` My actual code is ``` 'use strict'; angular.module('btford.markdown', []). directive('markdown', function () { var converter = new Showdown.converter(); return { restrict: 'A', link: function (scope, element, attrs) { scope.$watch(element.html(), function(value) { var htmlText = converter.makeHtml(element.html()); element.html(htmlText); }); var htmlText = converter.makeHtml(element.text()); element.html(htmlText); } } }); ```
2013/05/16
[ "https://Stackoverflow.com/questions/16590765", "https://Stackoverflow.com", "https://Stackoverflow.com/users/2390493/" ]
It may be cleaner for you to use the `$stateChangeSuccess` event inside your directive rather than setting your own $watch. Try adding a callback function to the `$stateChangeSuccess` event, this should trickle down to the scope of your directive. ``` 'use strict'; angular.module('btford.markdown', []). directive('markdown', function () { var converter = new Showdown.converter(); return { restrict: 'A', link: function (scope, element, attrs) { // When the state is change to, or reloaded... scope.$on('$stateChangeSuccess', function () { var htmlText = converter.makeHtml(element.text()); element.html(htmlText); }); } } }); ```
You can only watch variables on your scope. ``` scope.foo = 'bar'; scope.$watch('foo', function(newValue) { // Code to execute here }); ``` If you want to monitor the changes of DOM elements you need to do this on your own.
16,590,765
I am looking to create a **mardown directive** (restrict A) which would make me able to use same recipient for **ng-view**. So I would basically load only .md files in views and apply my function on its content each time ng-view change. So : **index.html** ``` <div markdown ng-view></div> ``` with two views containing, let say, **view1.md** ``` #That should be h1 ``` and **view2.md** ``` ##That should be h2, no ? ``` My actual code is ``` 'use strict'; angular.module('btford.markdown', []). directive('markdown', function () { var converter = new Showdown.converter(); return { restrict: 'A', link: function (scope, element, attrs) { scope.$watch(element.html(), function(value) { var htmlText = converter.makeHtml(element.html()); element.html(htmlText); }); var htmlText = converter.makeHtml(element.text()); element.html(htmlText); } } }); ```
2013/05/16
[ "https://Stackoverflow.com/questions/16590765", "https://Stackoverflow.com", "https://Stackoverflow.com/users/2390493/" ]
The first param of watch can be a function, return any value you want including your $element.html(). You can even do a combination of data ``` $scope.$watch( function() { return $element.attr("abc") + $scope.variable + someinternalvar; }, function(newVal, oldVal) { doTheStuff(); } ); ``` Obviously the more intense the data you put in here the slower your watches will be. Use caution. -- FYI You should clean up your watchers, create an array and push the results of $scope.$watch into that array. Then on the $destroy message remove them. Also remember to unbind events as they will cause eventual performance issues as scopes are created & destroyed. ``` $document.bind('click', clickMe); $(window).on("resize", winResize); var watches = [] watches.push($scope.$watch("thing", function() { Thing(); })); $scope.$on("$destroy", function () { for (var i in watches) watches[i](); $document.unbind('click', clickMe); $(window).off("resize", winResize); }); ``` -- EDIT 2016-07-14 Just to add, cleaning up scope watchers is not needed as they are already processed internally, however rootScope, parent, etc. you should absolutely cleanup.
It may be cleaner for you to use the `$stateChangeSuccess` event inside your directive rather than setting your own $watch. Try adding a callback function to the `$stateChangeSuccess` event, this should trickle down to the scope of your directive. ``` 'use strict'; angular.module('btford.markdown', []). directive('markdown', function () { var converter = new Showdown.converter(); return { restrict: 'A', link: function (scope, element, attrs) { // When the state is change to, or reloaded... scope.$on('$stateChangeSuccess', function () { var htmlText = converter.makeHtml(element.text()); element.html(htmlText); }); } } }); ```
64,590
This question makes absolutely no sense to me and it is driving me crazy that I cannot find a solution. [![enter image description here](https://i.stack.imgur.com/iZSzh.png)](https://i.stack.imgur.com/iZSzh.png)
2018/04/22
[ "https://puzzling.stackexchange.com/questions/64590", "https://puzzling.stackexchange.com", "https://puzzling.stackexchange.com/users/48438/" ]
it's > > green, blank, blank. > > > because... calling the cells A,B,C,D,E,F and examples 1,2,3,4,5 1-2 deonstrates that B is the opposite of D It can also be deduced by looking at 1,2,3,4,5 that A,B,C are not correlated with each other then it can be seen that A always matches F and C always matces E leaving only one of the patterns offered as the answer. 3 and 4 identical hints that there is no vertical sequence 5 being half filled suggests that the D,E,F are can be determined by A,B,C it took me a lot longer to deduce this than the time I allow to answer most IQ test questions.
Extracting a pattern from so little data will always be ambiguous. My guess would be > > Green, blank, blank > > > because if we assume there is an independent operation for deducing the fifth element in a column from the other four, we have existing samples for the fifth and sixth column, and only one answer choice that fits both.
64,590
This question makes absolutely no sense to me and it is driving me crazy that I cannot find a solution. [![enter image description here](https://i.stack.imgur.com/iZSzh.png)](https://i.stack.imgur.com/iZSzh.png)
2018/04/22
[ "https://puzzling.stackexchange.com/questions/64590", "https://puzzling.stackexchange.com", "https://puzzling.stackexchange.com/users/48438/" ]
it's > > green, blank, blank. > > > because... calling the cells A,B,C,D,E,F and examples 1,2,3,4,5 1-2 deonstrates that B is the opposite of D It can also be deduced by looking at 1,2,3,4,5 that A,B,C are not correlated with each other then it can be seen that A always matches F and C always matces E leaving only one of the patterns offered as the answer. 3 and 4 identical hints that there is no vertical sequence 5 being half filled suggests that the D,E,F are can be determined by A,B,C it took me a lot longer to deduce this than the time I allow to answer most IQ test questions.
> > I notice that there are 3 shaded (blue) blocks and 3 non-shaded blocks per row. > > From this alone, I can tell that the likely solution is(final row): > > u = un-shaded, s = shaded > > **Given**: u, u, u; > > **Solved**: s, s, s; > > **Final**: u, u, u, s, s, s > > > **[INCORRECT]**
64,590
This question makes absolutely no sense to me and it is driving me crazy that I cannot find a solution. [![enter image description here](https://i.stack.imgur.com/iZSzh.png)](https://i.stack.imgur.com/iZSzh.png)
2018/04/22
[ "https://puzzling.stackexchange.com/questions/64590", "https://puzzling.stackexchange.com", "https://puzzling.stackexchange.com/users/48438/" ]
it's > > green, blank, blank. > > > because... calling the cells A,B,C,D,E,F and examples 1,2,3,4,5 1-2 deonstrates that B is the opposite of D It can also be deduced by looking at 1,2,3,4,5 that A,B,C are not correlated with each other then it can be seen that A always matches F and C always matces E leaving only one of the patterns offered as the answer. 3 and 4 identical hints that there is no vertical sequence 5 being half filled suggests that the D,E,F are can be determined by A,B,C it took me a lot longer to deduce this than the time I allow to answer most IQ test questions.
If we treat the blocks as pixels which move on a small screen, and roll over from one side to the other, as they go off the edge of the screen, then we get the answer: > > blank,green,green > > > as we can see fairly quickly that the two pixels on the left each move left one space to the left, and then wrap around to the right, and the pixel on the left moves 0, then 1, then 2, then 3 spaces. Not sure if this is how to interpret this question (I have noticed some IQ questions are just poorly designed), but This is at least a logically consistant answer
64,590
This question makes absolutely no sense to me and it is driving me crazy that I cannot find a solution. [![enter image description here](https://i.stack.imgur.com/iZSzh.png)](https://i.stack.imgur.com/iZSzh.png)
2018/04/22
[ "https://puzzling.stackexchange.com/questions/64590", "https://puzzling.stackexchange.com", "https://puzzling.stackexchange.com/users/48438/" ]
Extracting a pattern from so little data will always be ambiguous. My guess would be > > Green, blank, blank > > > because if we assume there is an independent operation for deducing the fifth element in a column from the other four, we have existing samples for the fifth and sixth column, and only one answer choice that fits both.
> > I notice that there are 3 shaded (blue) blocks and 3 non-shaded blocks per row. > > From this alone, I can tell that the likely solution is(final row): > > u = un-shaded, s = shaded > > **Given**: u, u, u; > > **Solved**: s, s, s; > > **Final**: u, u, u, s, s, s > > > **[INCORRECT]**
64,590
This question makes absolutely no sense to me and it is driving me crazy that I cannot find a solution. [![enter image description here](https://i.stack.imgur.com/iZSzh.png)](https://i.stack.imgur.com/iZSzh.png)
2018/04/22
[ "https://puzzling.stackexchange.com/questions/64590", "https://puzzling.stackexchange.com", "https://puzzling.stackexchange.com/users/48438/" ]
Extracting a pattern from so little data will always be ambiguous. My guess would be > > Green, blank, blank > > > because if we assume there is an independent operation for deducing the fifth element in a column from the other four, we have existing samples for the fifth and sixth column, and only one answer choice that fits both.
If we treat the blocks as pixels which move on a small screen, and roll over from one side to the other, as they go off the edge of the screen, then we get the answer: > > blank,green,green > > > as we can see fairly quickly that the two pixels on the left each move left one space to the left, and then wrap around to the right, and the pixel on the left moves 0, then 1, then 2, then 3 spaces. Not sure if this is how to interpret this question (I have noticed some IQ questions are just poorly designed), but This is at least a logically consistant answer
593,110
So "half" belongs to a special class of words known as "predeterminers", those that can occur before determiners: > > Half a century > > > > > Half the people in this company can't speak a word of English > > > In English though, there are no indefinite (article-type) determiners in the plural: there are no plural equivalents to "a/an". This means that "half", as a predeterminer, does not have any determiner to "pre". > > Half Americans disapprove of the President (?) > > > > > Half Japanese women prefer men who can cook (?) > > > Are these two last sentences incorrect in any way? Or could they sound awkward or unnatural? Should I use "half of" instead?
2022/08/06
[ "https://english.stackexchange.com/questions/593110", "https://english.stackexchange.com", "https://english.stackexchange.com/users/120439/" ]
I had to go to the third page of my search results but [ELT Concourse](https://www.eltconcourse.com/training/inservice/lexicogrammar/pre-_post-determiners.html) claims precisely that: the contextual range for *half* is more restricted than *all* and *both*, both of which are also predeterminers. In short, unlike *all* and *both*, there **must** be a determiner after *half*. Therefore, such phrase as "half Americans" is incorrect, and must be changed to "half of Americans", for example. Here's an excerpt from the source: > > With plural count nouns we can use all three pre-determiners but *half* cannot be used with the zero article > > > For example: > > > * half those oranges are rotten > * both the children came > * all the men went home and stayed there > * all lions are unpredictable > * both (the) dogs are friendly > > > but not with the zero article: > > > * \*half people arrived > * \*half trains are always late in my country > > >
Just confirming a comment. Both of the sentences are acceptable, but *mean* different things. > > Half of Japanese women prefer men who can cook. > > > This means that in a given population, we are speaking of 50% or so. > > Half Japanese women prefer men who can cook. > > > This would imply the women in question are half Japanese. See [a CNN article on Japan's hafu](https://www.cnn.com/2020/09/22/asia/japan-mixed-roots-hafu-dst-hnk-intl/index.html): > > The Japanese word "hafu" -- or "half" in English -- refers to people who are ethnically half Japanese, and is now used more for multiethnic people in general in Japan. > > >
593,110
So "half" belongs to a special class of words known as "predeterminers", those that can occur before determiners: > > Half a century > > > > > Half the people in this company can't speak a word of English > > > In English though, there are no indefinite (article-type) determiners in the plural: there are no plural equivalents to "a/an". This means that "half", as a predeterminer, does not have any determiner to "pre". > > Half Americans disapprove of the President (?) > > > > > Half Japanese women prefer men who can cook (?) > > > Are these two last sentences incorrect in any way? Or could they sound awkward or unnatural? Should I use "half of" instead?
2022/08/06
[ "https://english.stackexchange.com/questions/593110", "https://english.stackexchange.com", "https://english.stackexchange.com/users/120439/" ]
After reading your question, I didn't quite understand what you're asking. Only after reading your own answer did I understand what you're asking and, more importantly, why you're asking what you're asking. To quote your own answer: > > the contextual range for *half* is more restricted than *all* and *both*, both of which are also predeterminers. In short, unlike *all* and *both*, there **must** be a determiner after *half*. > > > So, you're comparing *half* with *all* and *both*, which is simply comparing apples with oranges. Firstly, *half* is a fraction, just as *quarter*, *third*, and *fifth* are. Secondly, fractions can only function as predeterminers and cannot function as determiners, which means that they can come only before a determiner. So it's only natural that you can't say things like: > > \**Half Americans disapprove of the President.* > > > Because you don't have any determiner right after the predeterminer! In contrast, *both* and *all* can function as determiners as well as predeterminers. To quote the examples of your own answer: > > * half those oranges are rotten [predeterminer] > * *both* the children came [predeterminer] > * *all* the men went home and stayed there [predeterminer] > * *all* lions are unpredictable [determiner] > * *both* the dogs are friendly [predeterminer] > * *both* dogs are friendly [determiner] > * \**half* people arrived [predeterminer] > * \**half* trains are always late in my country [predeterminer] > > > So you just have to know that *half* is a fraction whereas *all* and *both* are not, and therefore that *half* functions differently from *all* and *both*.
Just confirming a comment. Both of the sentences are acceptable, but *mean* different things. > > Half of Japanese women prefer men who can cook. > > > This means that in a given population, we are speaking of 50% or so. > > Half Japanese women prefer men who can cook. > > > This would imply the women in question are half Japanese. See [a CNN article on Japan's hafu](https://www.cnn.com/2020/09/22/asia/japan-mixed-roots-hafu-dst-hnk-intl/index.html): > > The Japanese word "hafu" -- or "half" in English -- refers to people who are ethnically half Japanese, and is now used more for multiethnic people in general in Japan. > > >
593,110
So "half" belongs to a special class of words known as "predeterminers", those that can occur before determiners: > > Half a century > > > > > Half the people in this company can't speak a word of English > > > In English though, there are no indefinite (article-type) determiners in the plural: there are no plural equivalents to "a/an". This means that "half", as a predeterminer, does not have any determiner to "pre". > > Half Americans disapprove of the President (?) > > > > > Half Japanese women prefer men who can cook (?) > > > Are these two last sentences incorrect in any way? Or could they sound awkward or unnatural? Should I use "half of" instead?
2022/08/06
[ "https://english.stackexchange.com/questions/593110", "https://english.stackexchange.com", "https://english.stackexchange.com/users/120439/" ]
After reading your question, I didn't quite understand what you're asking. Only after reading your own answer did I understand what you're asking and, more importantly, why you're asking what you're asking. To quote your own answer: > > the contextual range for *half* is more restricted than *all* and *both*, both of which are also predeterminers. In short, unlike *all* and *both*, there **must** be a determiner after *half*. > > > So, you're comparing *half* with *all* and *both*, which is simply comparing apples with oranges. Firstly, *half* is a fraction, just as *quarter*, *third*, and *fifth* are. Secondly, fractions can only function as predeterminers and cannot function as determiners, which means that they can come only before a determiner. So it's only natural that you can't say things like: > > \**Half Americans disapprove of the President.* > > > Because you don't have any determiner right after the predeterminer! In contrast, *both* and *all* can function as determiners as well as predeterminers. To quote the examples of your own answer: > > * half those oranges are rotten [predeterminer] > * *both* the children came [predeterminer] > * *all* the men went home and stayed there [predeterminer] > * *all* lions are unpredictable [determiner] > * *both* the dogs are friendly [predeterminer] > * *both* dogs are friendly [determiner] > * \**half* people arrived [predeterminer] > * \**half* trains are always late in my country [predeterminer] > > > So you just have to know that *half* is a fraction whereas *all* and *both* are not, and therefore that *half* functions differently from *all* and *both*.
I had to go to the third page of my search results but [ELT Concourse](https://www.eltconcourse.com/training/inservice/lexicogrammar/pre-_post-determiners.html) claims precisely that: the contextual range for *half* is more restricted than *all* and *both*, both of which are also predeterminers. In short, unlike *all* and *both*, there **must** be a determiner after *half*. Therefore, such phrase as "half Americans" is incorrect, and must be changed to "half of Americans", for example. Here's an excerpt from the source: > > With plural count nouns we can use all three pre-determiners but *half* cannot be used with the zero article > > > For example: > > > * half those oranges are rotten > * both the children came > * all the men went home and stayed there > * all lions are unpredictable > * both (the) dogs are friendly > > > but not with the zero article: > > > * \*half people arrived > * \*half trains are always late in my country > > >
7,321,698
I have used DATEDIFF function but since it requires yyyy/mm/dd this code is giving wrong value ,because i have stored date from date time picker in this formate day,month,dd,yyyy ``` private void button3_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); DBHandling db = new DBHandling(); db.GetDataset(" select JoiningDate from Emp where name = '" + textBox2.Text + "'", ref ds); db.GetDataset(" select LeavingDate from AddDate where name = '" + textBox2.Text + "'", ref ds1); string date1, date2; date1 = ds.Tables[0].Rows[0][0].ToString(); date2 = ds1.Tables[0].Rows[0][0].ToString(); db.GetDataset("select DATEDIFF(day,2008/1/15,2008/1/12) ", ref ds2); textBox5.Text = ds2.Tables[0].Rows[0][0].ToString(); } ``` so how should i do this so that i will gate right Difference between 2 dates i.e number of days between 2 dates,thanks in advance
2011/09/06
[ "https://Stackoverflow.com/questions/7321698", "https://Stackoverflow.com", "https://Stackoverflow.com/users/895965/" ]
Some time ago I was also studying SHA-256 and created pure-python class that implements this hash. If I remember correctly, mostly I've taken algorithm from Wikipedia [SHA-256 Pseudocode](https://en.wikipedia.org/wiki/SHA-2#Pseudocode) and partially from some open-source projects. Algorithm doesn't import any (even standard) modules. Of cause it is much slower than `hashlib`'s variant and only meant for studying. If you just run the script it executes 1000 tests comparing `hashlib`'s and my variants. Only testing function imports some modules, algorithm's class itself doesn't need any modules. Interface is same as in hashlib's sha256 class. See `test()` function for examples of usage. [Try it online!](https://tio.run/##lVZNc9s2EL37V6CHVmRtKwAIgKQnyfTk6aW9pDdV1QAgYDEWJY9IT2VPp3/dXSwoiZRlN@FF0AL7dvftB/Dw1C036@zlxa5025IvS82lurkg8N235BOZ4TJ8dCd4obkviytY5yzLhShZWBtpqTfWh7UrjayMllcDvayUynJpwr4sPWPMo17JM19wLcJaG2alq0Z6VUFzraM9xotMGop6XGSskMYhnqQ2r2w21Mu5cbLKEbeglTPM49nSVJYqnYe1ZaXxDM4M9JwojSot2nDeOJEXKqyph9OVVdE2tZpZO9TjlSu5VRi/0LkohNbomzVUgyLypYC4otJDvbLInGSSY/xFxqxSFfJJacZzi3EbL8vc23yoZ5Wj1Pgs7FdS5yUTGBNVVqtMov9M8JKXaqTHc5NTXUiMwzHDWYY2RAXeVx79lFkGlLERn0pSnWdSxDiUptpgLgtmuS155JbnnNtilD/NjXeFZjE@ppUSqGe5MAW4gutcWcn0yF7FALRgJcanypIqjra9oC6T0X8GidQUMAZ6rNTCMoZ5Yi4DZIrxcUhJnguMLxOGGmtO6pNZag3yKVxVaC1i/kxprRaYV1Vwp7wf15kooH4dxp8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MXdDPrga9J0q9@RAvzI8dVfefaDjkeEzzk6O3LF9fhjj1QkaavD0cj8ckw/bqp18nueK2K0RTeBWL6Arkp5mfA4mvhj@2jO71EBraOAS7d7nyMnQ6tMKGMZ0KqvIB3lAWFySnq3mU4PzNhhorQ1vFfeD/cztFxc3C8NwdEXAQHOvwT7dbNw2YbeLUA3kJvQOWtaugZKIVq01wcyiMoHUtEUjpwHIjXWFwIAkTeu3XPZYSZhp@oyWU@SEjozzC6otVpi0@BJOCl0yNPcOt9eWPntJF06DlwH2dt@m3un3fiaDQdNcrXI8yr4OAyO@mW76GG8fKkVvW@qjHu8dQ8v9XHosfsme@k7GELb5RkEuPHeoHp0z5aCy3vH1erpym8OS/q8Ghf68YtFgFislg0ul4vFpNIQSyzi5eX/wA "Python 3 – Try It Online") ``` class Sha256: ks = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ] hs = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ] M32 = 0xFFFFFFFF def __init__(self, m = None): self.mlen = 0 self.buf = b'' self.k = self.ks[:] self.h = self.hs[:] self.fin = False if m is not None: self.update(m) @staticmethod def pad(mlen): mdi = mlen & 0x3F length = (mlen << 3).to_bytes(8, 'big') padlen = 55 - mdi if mdi < 56 else 119 - mdi return b'\x80' + b'\x00' * padlen + length @staticmethod def ror(x, y): return ((x >> y) | (x << (32 - y))) & Sha256.M32 @staticmethod def maj(x, y, z): return (x & y) ^ (x & z) ^ (y & z) @staticmethod def ch(x, y, z): return (x & y) ^ ((~x) & z) def compress(self, c): w = [0] * 64 w[0 : 16] = [int.from_bytes(c[i : i + 4], 'big') for i in range(0, len(c), 4)] for i in range(16, 64): s0 = self.ror(w[i - 15], 7) ^ self.ror(w[i - 15], 18) ^ (w[i - 15] >> 3) s1 = self.ror(w[i - 2], 17) ^ self.ror(w[i - 2], 19) ^ (w[i - 2] >> 10) w[i] = (w[i - 16] + s0 + w[i - 7] + s1) & self.M32 a, b, c, d, e, f, g, h = self.h for i in range(64): s0 = self.ror(a, 2) ^ self.ror(a, 13) ^ self.ror(a, 22) t2 = s0 + self.maj(a, b, c) s1 = self.ror(e, 6) ^ self.ror(e, 11) ^ self.ror(e, 25) t1 = h + s1 + self.ch(e, f, g) + self.k[i] + w[i] h = g g = f f = e e = (d + t1) & self.M32 d = c c = b b = a a = (t1 + t2) & self.M32 for i, (x, y) in enumerate(zip(self.h, [a, b, c, d, e, f, g, h])): self.h[i] = (x + y) & self.M32 def update(self, m): if m is None or len(m) == 0: return assert not self.fin, 'Hash already finalized and can not be updated!' self.mlen += len(m) m = self.buf + m for i in range(0, len(m) // 64): self.compress(m[64 * i : 64 * (i + 1)]) self.buf = m[len(m) - (len(m) % 64):] def digest(self): if not self.fin: self.update(self.pad(self.mlen)) self.digest = b''.join(x.to_bytes(4, 'big') for x in self.h[:8]) self.fin = True return self.digest def hexdigest(self): tab = '0123456789abcdef' return ''.join(tab[b >> 4] + tab[b & 0xF] for b in self.digest()) def test(): import secrets, hashlib, random for itest in range(500): data = secrets.token_bytes(random.randrange(257)) a, b = hashlib.sha256(data).hexdigest(), Sha256(data).hexdigest() assert a == b, (a, b) for itest in range(500): a, b = hashlib.sha256(), Sha256() for j in range(random.randrange(10)): data = secrets.token_bytes(random.randrange(129)) a.update(data) b.update(data) a, b = a.hexdigest(), b.hexdigest() assert a == b, (a, b) print('Sha256 tested successfully.') if __name__ == '__main__': test() ```
Translating <http://en.wikipedia.org/wiki/SHA-2#SHA-256_.28a_SHA-2_variant.29_pseudocode> to Python should be straight forward.
7,321,698
I have used DATEDIFF function but since it requires yyyy/mm/dd this code is giving wrong value ,because i have stored date from date time picker in this formate day,month,dd,yyyy ``` private void button3_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); DBHandling db = new DBHandling(); db.GetDataset(" select JoiningDate from Emp where name = '" + textBox2.Text + "'", ref ds); db.GetDataset(" select LeavingDate from AddDate where name = '" + textBox2.Text + "'", ref ds1); string date1, date2; date1 = ds.Tables[0].Rows[0][0].ToString(); date2 = ds1.Tables[0].Rows[0][0].ToString(); db.GetDataset("select DATEDIFF(day,2008/1/15,2008/1/12) ", ref ds2); textBox5.Text = ds2.Tables[0].Rows[0][0].ToString(); } ``` so how should i do this so that i will gate right Difference between 2 dates i.e number of days between 2 dates,thanks in advance
2011/09/06
[ "https://Stackoverflow.com/questions/7321698", "https://Stackoverflow.com", "https://Stackoverflow.com/users/895965/" ]
PyPy's source contains a pure-python implementation of SHA-256 [here](https://foss.heptapod.net/pypy/pypy/-/blob/branch/default/lib_pypy/_sha256.py). Poking around in that directory, you'll probably also find pure-python implementations of other standard hashes.
Translating <http://en.wikipedia.org/wiki/SHA-2#SHA-256_.28a_SHA-2_variant.29_pseudocode> to Python should be straight forward.
7,321,698
I have used DATEDIFF function but since it requires yyyy/mm/dd this code is giving wrong value ,because i have stored date from date time picker in this formate day,month,dd,yyyy ``` private void button3_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); DBHandling db = new DBHandling(); db.GetDataset(" select JoiningDate from Emp where name = '" + textBox2.Text + "'", ref ds); db.GetDataset(" select LeavingDate from AddDate where name = '" + textBox2.Text + "'", ref ds1); string date1, date2; date1 = ds.Tables[0].Rows[0][0].ToString(); date2 = ds1.Tables[0].Rows[0][0].ToString(); db.GetDataset("select DATEDIFF(day,2008/1/15,2008/1/12) ", ref ds2); textBox5.Text = ds2.Tables[0].Rows[0][0].ToString(); } ``` so how should i do this so that i will gate right Difference between 2 dates i.e number of days between 2 dates,thanks in advance
2011/09/06
[ "https://Stackoverflow.com/questions/7321698", "https://Stackoverflow.com", "https://Stackoverflow.com/users/895965/" ]
Some time ago I was also studying SHA-256 and created pure-python class that implements this hash. If I remember correctly, mostly I've taken algorithm from Wikipedia [SHA-256 Pseudocode](https://en.wikipedia.org/wiki/SHA-2#Pseudocode) and partially from some open-source projects. Algorithm doesn't import any (even standard) modules. Of cause it is much slower than `hashlib`'s variant and only meant for studying. If you just run the script it executes 1000 tests comparing `hashlib`'s and my variants. Only testing function imports some modules, algorithm's class itself doesn't need any modules. Interface is same as in hashlib's sha256 class. See `test()` function for examples of usage. [Try it online!](https://tio.run/##lVZNc9s2EL37V6CHVmRtKwAIgKQnyfTk6aW9pDdV1QAgYDEWJY9IT2VPp3/dXSwoiZRlN@FF0AL7dvftB/Dw1C036@zlxa5025IvS82lurkg8N235BOZ4TJ8dCd4obkviytY5yzLhShZWBtpqTfWh7UrjayMllcDvayUynJpwr4sPWPMo17JM19wLcJaG2alq0Z6VUFzraM9xotMGop6XGSskMYhnqQ2r2w21Mu5cbLKEbeglTPM49nSVJYqnYe1ZaXxDM4M9JwojSot2nDeOJEXKqyph9OVVdE2tZpZO9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bjZBBwNiphMsTWwFjiczs7UFt9xSke9F9oBaJHFQQXgugfTeS3IHEjyRhvrqRxIU0VQDYjdMxPFPBGTuS2DCnRxIDEj2S6IDchVA6fj7TmMArEgdOSKRbPzZuG6bvc/2QxHxfkdn5ipin6Znpvewrbwd2n16bDW3fD/j@hhqA7C@HcDEQ8C00a5OST3BRjS3F6TIo2bZ12w4vlf3NAzPgV90uiV5tna6eCIj0qn52FdHrili9xtPG9d5UP0wuzlyRl596J463zL7OwmV5SZo326EfNuD/hw9nxglW137@NTMlYNSFCYaLJMwxls7Ti3MXdDPrga9J0q9@RAvzI8dVfefaDjkeEzzk6O3LF9fhjj1QkaavD0cj8ckw/bqp18nueK2K0RTeBWL6Arkp5mfA4mvhj@2jO71EBraOAS7d7nyMnQ6tMKGMZ0KqvIB3lAWFySnq3mU4PzNhhorQ1vFfeD/cztFxc3C8NwdEXAQHOvwT7dbNw2YbeLUA3kJvQOWtaugZKIVq01wcyiMoHUtEUjpwHIjXWFwIAkTeu3XPZYSZhp@oyWU@SEjozzC6otVpi0@BJOCl0yNPcOt9eWPntJF06DlwH2dt@m3un3fiaDQdNcrXI8yr4OAyO@mW76GG8fKkVvW@qjHu8dQ8v9XHosfsme@k7GELb5RkEuPHeoHp0z5aCy3vH1erpym8OS/q8Ghf68YtFgFislg0ul4vFpNIQSyzi5eX/wA "Python 3 – Try It Online") ``` class Sha256: ks = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ] hs = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ] M32 = 0xFFFFFFFF def __init__(self, m = None): self.mlen = 0 self.buf = b'' self.k = self.ks[:] self.h = self.hs[:] self.fin = False if m is not None: self.update(m) @staticmethod def pad(mlen): mdi = mlen & 0x3F length = (mlen << 3).to_bytes(8, 'big') padlen = 55 - mdi if mdi < 56 else 119 - mdi return b'\x80' + b'\x00' * padlen + length @staticmethod def ror(x, y): return ((x >> y) | (x << (32 - y))) & Sha256.M32 @staticmethod def maj(x, y, z): return (x & y) ^ (x & z) ^ (y & z) @staticmethod def ch(x, y, z): return (x & y) ^ ((~x) & z) def compress(self, c): w = [0] * 64 w[0 : 16] = [int.from_bytes(c[i : i + 4], 'big') for i in range(0, len(c), 4)] for i in range(16, 64): s0 = self.ror(w[i - 15], 7) ^ self.ror(w[i - 15], 18) ^ (w[i - 15] >> 3) s1 = self.ror(w[i - 2], 17) ^ self.ror(w[i - 2], 19) ^ (w[i - 2] >> 10) w[i] = (w[i - 16] + s0 + w[i - 7] + s1) & self.M32 a, b, c, d, e, f, g, h = self.h for i in range(64): s0 = self.ror(a, 2) ^ self.ror(a, 13) ^ self.ror(a, 22) t2 = s0 + self.maj(a, b, c) s1 = self.ror(e, 6) ^ self.ror(e, 11) ^ self.ror(e, 25) t1 = h + s1 + self.ch(e, f, g) + self.k[i] + w[i] h = g g = f f = e e = (d + t1) & self.M32 d = c c = b b = a a = (t1 + t2) & self.M32 for i, (x, y) in enumerate(zip(self.h, [a, b, c, d, e, f, g, h])): self.h[i] = (x + y) & self.M32 def update(self, m): if m is None or len(m) == 0: return assert not self.fin, 'Hash already finalized and can not be updated!' self.mlen += len(m) m = self.buf + m for i in range(0, len(m) // 64): self.compress(m[64 * i : 64 * (i + 1)]) self.buf = m[len(m) - (len(m) % 64):] def digest(self): if not self.fin: self.update(self.pad(self.mlen)) self.digest = b''.join(x.to_bytes(4, 'big') for x in self.h[:8]) self.fin = True return self.digest def hexdigest(self): tab = '0123456789abcdef' return ''.join(tab[b >> 4] + tab[b & 0xF] for b in self.digest()) def test(): import secrets, hashlib, random for itest in range(500): data = secrets.token_bytes(random.randrange(257)) a, b = hashlib.sha256(data).hexdigest(), Sha256(data).hexdigest() assert a == b, (a, b) for itest in range(500): a, b = hashlib.sha256(), Sha256() for j in range(random.randrange(10)): data = secrets.token_bytes(random.randrange(129)) a.update(data) b.update(data) a, b = a.hexdigest(), b.hexdigest() assert a == b, (a, b) print('Sha256 tested successfully.') if __name__ == '__main__': test() ```
If you only want the hash value: ``` from hashlib import sha256 data = input('Enter plaintext data: ') output = sha256(data.encode('utf-8')) print(output) ``` Python's hashlib also has SHA-1, SHA-384, SHA-512, and MD5 hash functions.
7,321,698
I have used DATEDIFF function but since it requires yyyy/mm/dd this code is giving wrong value ,because i have stored date from date time picker in this formate day,month,dd,yyyy ``` private void button3_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); DBHandling db = new DBHandling(); db.GetDataset(" select JoiningDate from Emp where name = '" + textBox2.Text + "'", ref ds); db.GetDataset(" select LeavingDate from AddDate where name = '" + textBox2.Text + "'", ref ds1); string date1, date2; date1 = ds.Tables[0].Rows[0][0].ToString(); date2 = ds1.Tables[0].Rows[0][0].ToString(); db.GetDataset("select DATEDIFF(day,2008/1/15,2008/1/12) ", ref ds2); textBox5.Text = ds2.Tables[0].Rows[0][0].ToString(); } ``` so how should i do this so that i will gate right Difference between 2 dates i.e number of days between 2 dates,thanks in advance
2011/09/06
[ "https://Stackoverflow.com/questions/7321698", "https://Stackoverflow.com", "https://Stackoverflow.com/users/895965/" ]
PyPy's source contains a pure-python implementation of SHA-256 [here](https://foss.heptapod.net/pypy/pypy/-/blob/branch/default/lib_pypy/_sha256.py). Poking around in that directory, you'll probably also find pure-python implementations of other standard hashes.
Some time ago I was also studying SHA-256 and created pure-python class that implements this hash. If I remember correctly, mostly I've taken algorithm from Wikipedia [SHA-256 Pseudocode](https://en.wikipedia.org/wiki/SHA-2#Pseudocode) and partially from some open-source projects. Algorithm doesn't import any (even standard) modules. Of cause it is much slower than `hashlib`'s variant and only meant for studying. If you just run the script it executes 1000 tests comparing `hashlib`'s and my variants. Only testing function imports some modules, algorithm's class itself doesn't need any modules. Interface is same as in hashlib's sha256 class. See `test()` function for examples of usage. [Try it online!](https://tio.run/##lVZNc9s2EL37V6CHVmRtKwAIgKQnyfTk6aW9pDdV1QAgYDEWJY9IT2VPp3/dXSwoiZRlN@FF0AL7dvftB/Dw1C036@zlxa5025IvS82lurkg8N235BOZ4TJ8dCd4obkviytY5yzLhShZWBtpqTfWh7UrjayMllcDvayUynJpwr4sPWPMo17JM19wLcJaG2alq0Z6VUFzraM9xotMGop6XGSskMYhnqQ2r2w21Mu5cbLKEbeglTPM49nSVJYqnYe1ZaXxDM4M9JwojSot2nDeOJEXKqyph9OVVdE2tZpZO9TjlSu5VRi/0LkohNbomzVUgyLypYC4otJDvbLInGSSY/xFxqxSFfJJacZzi3EbL8vc23yoZ5Wj1Pgs7FdS5yUTGBNVVqtMov9M8JKXaqTHc5NTXUiMwzHDWYY2RAXeVx79lFkGlLERn0pSnWdSxDiUptpgLgtmuS155JbnnNtilD/NjXeFZjE@ppUSqGe5MAW4gutcWcn0yF7FALRgJcanypIqjra9oC6T0X8Gid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j0qn52FdHrili9xtPG9d5UP0wuzlyRl596J463zL7OwmV5SZo326EfNuD/hw9nxglW137@NTMlYNSFCYaLJMwxls7Ti3MXdDPrga9J0q9@RAvzI8dVfefaDjkeEzzk6O3LF9fhjj1QkaavD0cj8ckw/bqp18nueK2K0RTeBWL6Arkp5mfA4mvhj@2jO71EBraOAS7d7nyMnQ6tMKGMZ0KqvIB3lAWFySnq3mU4PzNhhorQ1vFfeD/cztFxc3C8NwdEXAQHOvwT7dbNw2YbeLUA3kJvQOWtaugZKIVq01wcyiMoHUtEUjpwHIjXWFwIAkTeu3XPZYSZhp@oyWU@SEjozzC6otVpi0@BJOCl0yNPcOt9eWPntJF06DlwH2dt@m3un3fiaDQdNcrXI8yr4OAyO@mW76GG8fKkVvW@qjHu8dQ8v9XHosfsme@k7GELb5RkEuPHeoHp0z5aCy3vH1erpym8OS/q8Ghf68YtFgFislg0ul4vFpNIQSyzi5eX/wA "Python 3 – Try It Online") ``` class Sha256: ks = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ] hs = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ] M32 = 0xFFFFFFFF def __init__(self, m = None): self.mlen = 0 self.buf = b'' self.k = self.ks[:] self.h = self.hs[:] self.fin = False if m is not None: self.update(m) @staticmethod def pad(mlen): mdi = mlen & 0x3F length = (mlen << 3).to_bytes(8, 'big') padlen = 55 - mdi if mdi < 56 else 119 - mdi return b'\x80' + b'\x00' * padlen + length @staticmethod def ror(x, y): return ((x >> y) | (x << (32 - y))) & Sha256.M32 @staticmethod def maj(x, y, z): return (x & y) ^ (x & z) ^ (y & z) @staticmethod def ch(x, y, z): return (x & y) ^ ((~x) & z) def compress(self, c): w = [0] * 64 w[0 : 16] = [int.from_bytes(c[i : i + 4], 'big') for i in range(0, len(c), 4)] for i in range(16, 64): s0 = self.ror(w[i - 15], 7) ^ self.ror(w[i - 15], 18) ^ (w[i - 15] >> 3) s1 = self.ror(w[i - 2], 17) ^ self.ror(w[i - 2], 19) ^ (w[i - 2] >> 10) w[i] = (w[i - 16] + s0 + w[i - 7] + s1) & self.M32 a, b, c, d, e, f, g, h = self.h for i in range(64): s0 = self.ror(a, 2) ^ self.ror(a, 13) ^ self.ror(a, 22) t2 = s0 + self.maj(a, b, c) s1 = self.ror(e, 6) ^ self.ror(e, 11) ^ self.ror(e, 25) t1 = h + s1 + self.ch(e, f, g) + self.k[i] + w[i] h = g g = f f = e e = (d + t1) & self.M32 d = c c = b b = a a = (t1 + t2) & self.M32 for i, (x, y) in enumerate(zip(self.h, [a, b, c, d, e, f, g, h])): self.h[i] = (x + y) & self.M32 def update(self, m): if m is None or len(m) == 0: return assert not self.fin, 'Hash already finalized and can not be updated!' self.mlen += len(m) m = self.buf + m for i in range(0, len(m) // 64): self.compress(m[64 * i : 64 * (i + 1)]) self.buf = m[len(m) - (len(m) % 64):] def digest(self): if not self.fin: self.update(self.pad(self.mlen)) self.digest = b''.join(x.to_bytes(4, 'big') for x in self.h[:8]) self.fin = True return self.digest def hexdigest(self): tab = '0123456789abcdef' return ''.join(tab[b >> 4] + tab[b & 0xF] for b in self.digest()) def test(): import secrets, hashlib, random for itest in range(500): data = secrets.token_bytes(random.randrange(257)) a, b = hashlib.sha256(data).hexdigest(), Sha256(data).hexdigest() assert a == b, (a, b) for itest in range(500): a, b = hashlib.sha256(), Sha256() for j in range(random.randrange(10)): data = secrets.token_bytes(random.randrange(129)) a.update(data) b.update(data) a, b = a.hexdigest(), b.hexdigest() assert a == b, (a, b) print('Sha256 tested successfully.') if __name__ == '__main__': test() ```
7,321,698
I have used DATEDIFF function but since it requires yyyy/mm/dd this code is giving wrong value ,because i have stored date from date time picker in this formate day,month,dd,yyyy ``` private void button3_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); DBHandling db = new DBHandling(); db.GetDataset(" select JoiningDate from Emp where name = '" + textBox2.Text + "'", ref ds); db.GetDataset(" select LeavingDate from AddDate where name = '" + textBox2.Text + "'", ref ds1); string date1, date2; date1 = ds.Tables[0].Rows[0][0].ToString(); date2 = ds1.Tables[0].Rows[0][0].ToString(); db.GetDataset("select DATEDIFF(day,2008/1/15,2008/1/12) ", ref ds2); textBox5.Text = ds2.Tables[0].Rows[0][0].ToString(); } ``` so how should i do this so that i will gate right Difference between 2 dates i.e number of days between 2 dates,thanks in advance
2011/09/06
[ "https://Stackoverflow.com/questions/7321698", "https://Stackoverflow.com", "https://Stackoverflow.com/users/895965/" ]
Some time ago I was also studying SHA-256 and created pure-python class that implements this hash. If I remember correctly, mostly I've taken algorithm from Wikipedia [SHA-256 Pseudocode](https://en.wikipedia.org/wiki/SHA-2#Pseudocode) and partially from some open-source projects. Algorithm doesn't import any (even standard) modules. Of cause it is much slower than `hashlib`'s variant and only meant for studying. If you just run the script it executes 1000 tests comparing `hashlib`'s and my variants. Only testing function imports some modules, algorithm's class itself doesn't need any modules. Interface is same as in hashlib's sha256 class. See `test()` function for examples of usage. [Try it online!](https://tio.run/##lVZNc9s2EL37V6CHVmRtKwAIgKQnyfTk6aW9pDdV1QAgYDEWJY9IT2VPp3/dXSwoiZRlN@FF0AL7dvftB/Dw1C036@zlxa5025IvS82lurkg8N235BOZ4TJ8dCd4obkviytY5yzLhShZWBtpqTfWh7UrjayMllcDvayUynJpwr4sPWPMo17JM19wLcJaG2alq0Z6VUFzraM9xotMGop6XGSskMYhnqQ2r2w21Mu5cbLKEbeglTPM49nSVJYqnYe1ZaXxDM4M9JwojSot2nDeOJEXKqyph9OVVdE2tZpZO9TjlSu5VRi/0LkohNbomzVUgyLypYC4otJDvbLInGSSY/xFxqxSFfJJacZzi3EbL8vc23yoZ5Wj1Pgs7FdS5yUTGBNVVqtMov9M8JKXaqTHc5NTXUiMwzHDWYY2RAXeVx79lFkGlLERn0pSnWdSxDiUptpgLgtmuS155JbnnNtilD/NjXeFZjE@ppUSqGe5MAW4gutcWcn0yF7FALRgJcanypIqjra9oC6T0X8GidQUMAZ6rNTCMoZ5Yi4DZIrxcUhJnguMLxOGGmtO6pNZag3yKVxVaC1i/kxprRaYV1Vwp7wf15kooH4dxp8XWqos1kAhbJEXLNaftTnl4Mcw79Q47z3a0EJC2hzyAtJSZz7Wp8pZXnge9eYX@LM87UjIRemUQg1jVK5dZCeDEvFZHitLCu9lNqo8yaiTPPexK6hURYHsMF9kValjpxpHLaRi5MFvGQcX6O62/6K0cp4sFvW67haLpHUrwG3g2O@btUtvDmbDxrRZuXVAGEvNowehmUzG4nsQxkU7u5mP95b7veXrPV8HG7d61brDRu3Bp7ol602Hjh39Oqg9PlS6c0mTxqh@aTvd1bZxMCGrQ5wPukpCEIPAmqoGcxjZT4H828MOiO664CmqkI8fSZZOu83CPHWuTaA4J6a@m6SH8wAe@ZGSXCNucBt@PhK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ELb5RkEuPHeoHp0z5aCy3vH1erpym8OS/q8Ghf68YtFgFislg0ul4vFpNIQSyzi5eX/wA "Python 3 – Try It Online") ``` class Sha256: ks = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ] hs = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ] M32 = 0xFFFFFFFF def __init__(self, m = None): self.mlen = 0 self.buf = b'' self.k = self.ks[:] self.h = self.hs[:] self.fin = False if m is not None: self.update(m) @staticmethod def pad(mlen): mdi = mlen & 0x3F length = (mlen << 3).to_bytes(8, 'big') padlen = 55 - mdi if mdi < 56 else 119 - mdi return b'\x80' + b'\x00' * padlen + length @staticmethod def ror(x, y): return ((x >> y) | (x << (32 - y))) & Sha256.M32 @staticmethod def maj(x, y, z): return (x & y) ^ (x & z) ^ (y & z) @staticmethod def ch(x, y, z): return (x & y) ^ ((~x) & z) def compress(self, c): w = [0] * 64 w[0 : 16] = [int.from_bytes(c[i : i + 4], 'big') for i in range(0, len(c), 4)] for i in range(16, 64): s0 = self.ror(w[i - 15], 7) ^ self.ror(w[i - 15], 18) ^ (w[i - 15] >> 3) s1 = self.ror(w[i - 2], 17) ^ self.ror(w[i - 2], 19) ^ (w[i - 2] >> 10) w[i] = (w[i - 16] + s0 + w[i - 7] + s1) & self.M32 a, b, c, d, e, f, g, h = self.h for i in range(64): s0 = self.ror(a, 2) ^ self.ror(a, 13) ^ self.ror(a, 22) t2 = s0 + self.maj(a, b, c) s1 = self.ror(e, 6) ^ self.ror(e, 11) ^ self.ror(e, 25) t1 = h + s1 + self.ch(e, f, g) + self.k[i] + w[i] h = g g = f f = e e = (d + t1) & self.M32 d = c c = b b = a a = (t1 + t2) & self.M32 for i, (x, y) in enumerate(zip(self.h, [a, b, c, d, e, f, g, h])): self.h[i] = (x + y) & self.M32 def update(self, m): if m is None or len(m) == 0: return assert not self.fin, 'Hash already finalized and can not be updated!' self.mlen += len(m) m = self.buf + m for i in range(0, len(m) // 64): self.compress(m[64 * i : 64 * (i + 1)]) self.buf = m[len(m) - (len(m) % 64):] def digest(self): if not self.fin: self.update(self.pad(self.mlen)) self.digest = b''.join(x.to_bytes(4, 'big') for x in self.h[:8]) self.fin = True return self.digest def hexdigest(self): tab = '0123456789abcdef' return ''.join(tab[b >> 4] + tab[b & 0xF] for b in self.digest()) def test(): import secrets, hashlib, random for itest in range(500): data = secrets.token_bytes(random.randrange(257)) a, b = hashlib.sha256(data).hexdigest(), Sha256(data).hexdigest() assert a == b, (a, b) for itest in range(500): a, b = hashlib.sha256(), Sha256() for j in range(random.randrange(10)): data = secrets.token_bytes(random.randrange(129)) a.update(data) b.update(data) a, b = a.hexdigest(), b.hexdigest() assert a == b, (a, b) print('Sha256 tested successfully.') if __name__ == '__main__': test() ```
Here is my proposition with redis: ``` for i in range(len(rserver.keys())): mdp_hash = rserver.get(rserver.keys()[i]) rserver.set(rserver.keys()[i], hashlib.sha256(mdp_hash.encode()).hexdigest()) ```
7,321,698
I have used DATEDIFF function but since it requires yyyy/mm/dd this code is giving wrong value ,because i have stored date from date time picker in this formate day,month,dd,yyyy ``` private void button3_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); DBHandling db = new DBHandling(); db.GetDataset(" select JoiningDate from Emp where name = '" + textBox2.Text + "'", ref ds); db.GetDataset(" select LeavingDate from AddDate where name = '" + textBox2.Text + "'", ref ds1); string date1, date2; date1 = ds.Tables[0].Rows[0][0].ToString(); date2 = ds1.Tables[0].Rows[0][0].ToString(); db.GetDataset("select DATEDIFF(day,2008/1/15,2008/1/12) ", ref ds2); textBox5.Text = ds2.Tables[0].Rows[0][0].ToString(); } ``` so how should i do this so that i will gate right Difference between 2 dates i.e number of days between 2 dates,thanks in advance
2011/09/06
[ "https://Stackoverflow.com/questions/7321698", "https://Stackoverflow.com", "https://Stackoverflow.com/users/895965/" ]
``` initial_hash_values=[ '6a09e667','bb67ae85','3c6ef372','a54ff53a', '510e527f','9b05688c','1f83d9ab','5be0cd19' ] sha_256_constants=[ '428a2f98','71374491','b5c0fbcf','e9b5dba5', '3956c25b','59f111f1','923f82a4','ab1c5ed5', 'd807aa98','12835b01','243185be','550c7dc3', '72be5d74','80deb1fe','9bdc06a7','c19bf174', 'e49b69c1','efbe4786','0fc19dc6','240ca1cc', '2de92c6f','4a7484aa','5cb0a9dc','76f988da', '983e5152','a831c66d','b00327c8','bf597fc7', 'c6e00bf3','d5a79147','06ca6351','14292967', '27b70a85','2e1b2138','4d2c6dfc','53380d13', '650a7354','766a0abb','81c2c92e','92722c85', 'a2bfe8a1','a81a664b','c24b8b70','c76c51a3', 'd192e819','d6990624','f40e3585','106aa070', '19a4c116','1e376c08','2748774c','34b0bcb5', '391c0cb3','4ed8aa4a','5b9cca4f','682e6ff3', '748f82ee','78a5636f','84c87814','8cc70208', '90befffa','a4506ceb','bef9a3f7','c67178f2' ] def bin_return(dec): return(str(format(dec,'b'))) def bin_8bit(dec): return(str(format(dec,'08b'))) def bin_32bit(dec): return(str(format(dec,'032b'))) def bin_64bit(dec): return(str(format(dec,'064b'))) def hex_return(dec): return(str(format(dec,'x'))) def dec_return_bin(bin_string): return(int(bin_string,2)) def dec_return_hex(hex_string): return(int(hex_string,16)) def L_P(SET,n): to_return=[] j=0 k=n while k<len(SET)+1: to_return.append(SET[j:k]) j=k k+=n return(to_return) def s_l(bit_string): bit_list=[] for i in range(len(bit_string)): bit_list.append(bit_string[i]) return(bit_list) def l_s(bit_list): bit_string='' for i in range(len(bit_list)): bit_string+=bit_list[i] return(bit_string) def rotate_right(bit_string,n): bit_list = s_l(bit_string) count=0 while count <= n-1: list_main=list(bit_list) var_0=list_main.pop(-1) list_main=list([var_0]+list_main) bit_list=list(list_main) count+=1 return(l_s(list_main)) def shift_right(bit_string,n): bit_list=s_l(bit_string) count=0 while count <= n-1: bit_list.pop(-1) count+=1 front_append=['0']*n return(l_s(front_append+bit_list)) def mod_32_addition(input_set): value=0 for i in range(len(input_set)): value+=input_set[i] mod_32 = 4294967296 return(value%mod_32) def xor_2str(bit_string_1,bit_string_2): xor_list=[] for i in range(len(bit_string_1)): if bit_string_1[i]=='0' and bit_string_2[i]=='0': xor_list.append('0') if bit_string_1[i]=='1' and bit_string_2[i]=='1': xor_list.append('0') if bit_string_1[i]=='0' and bit_string_2[i]=='1': xor_list.append('1') if bit_string_1[i]=='1' and bit_string_2[i]=='0': xor_list.append('1') return(l_s(xor_list)) def and_2str(bit_string_1,bit_string_2): and_list=[] for i in range(len(bit_string_1)): if bit_string_1[i]=='1' and bit_string_2[i]=='1': and_list.append('1') else: and_list.append('0') return(l_s(and_list)) def or_2str(bit_string_1,bit_string_2): or_list=[] for i in range(len(bit_string_1)): if bit_string_1[i]=='0' and bit_string_2[i]=='0': or_list.append('0') else: or_list.append('1') return(l_s(or_list)) def not_str(bit_string): not_list=[] for i in range(len(bit_string)): if bit_string[i]=='0': not_list.append('1') else: not_list.append('0') return(l_s(not_list)) ''' SHA-256 Specific Functions: ''' def Ch(x,y,z): return(xor_2str(and_2str(x,y),and_2str(not_str(x),z))) def Maj(x,y,z): return(xor_2str(xor_2str(and_2str(x,y),and_2str(x,z)),and_2str(y,z))) def e_0(x): return(xor_2str(xor_2str(rotate_right(x,2),rotate_right(x,13)),rotate_right(x,22))) def e_1(x): return(xor_2str(xor_2str(rotate_right(x,6),rotate_right(x,11)),rotate_right(x,25))) def s_0(x): return(xor_2str(xor_2str(rotate_right(x,7),rotate_right(x,18)),shift_right(x,3))) def s_1(x): return(xor_2str(xor_2str(rotate_right(x,17),rotate_right(x,19)),shift_right(x,10))) def message_pad(bit_list): pad_one = bit_list + '1' pad_len = len(pad_one) k=0 while ((pad_len+k)-448)%512 != 0: k+=1 back_append_0 = '0'*k back_append_1 = bin_64bit(len(bit_list)) return(pad_one+back_append_0+back_append_1) def message_bit_return(string_input): bit_list=[] for i in range(len(string_input)): bit_list.append(bin_8bit(ord(string_input[i]))) return(l_s(bit_list)) def message_pre_pro(input_string): bit_main = message_bit_return(input_string) return(message_pad(bit_main)) def message_parsing(input_string): return(L_P(message_pre_pro(input_string),32)) def message_schedule(index,w_t): new_word = bin_32bit(mod_32_addition([int(s_1(w_t[index-2]),2),int(w_t[index-7],2),int(s_0(w_t[index-15]),2),int(w_t[index-16],2)])) return(new_word) ''' This example of SHA_256 works for an input string <56 characters. ''' def sha_256(input_string): assert len(input_string) < 56, "This example of SHA_256 works for an input string <56 characters." w_t=message_parsing(input_string) a=bin_32bit(dec_return_hex(initial_hash_values[0])) b=bin_32bit(dec_return_hex(initial_hash_values[1])) c=bin_32bit(dec_return_hex(initial_hash_values[2])) d=bin_32bit(dec_return_hex(initial_hash_values[3])) e=bin_32bit(dec_return_hex(initial_hash_values[4])) f=bin_32bit(dec_return_hex(initial_hash_values[5])) g=bin_32bit(dec_return_hex(initial_hash_values[6])) h=bin_32bit(dec_return_hex(initial_hash_values[7])) for i in range(0,64): if i <= 15: t_1=mod_32_addition([int(h,2),int(e_1(e),2),int(Ch(e,f,g),2),int(sha_256_constants[i],16),int(w_t[i],2)]) t_2=mod_32_addition([int(e_0(a),2),int(Maj(a,b,c),2)]) h=g g=f f=e e=mod_32_addition([int(d,2),t_1]) d=c c=b b=a a=mod_32_addition([t_1,t_2]) a=bin_32bit(a) e=bin_32bit(e) if i > 15: w_t.append(message_schedule(i,w_t)) t_1=mod_32_addition([int(h,2),int(e_1(e),2),int(Ch(e,f,g),2),int(sha_256_constants[i],16),int(w_t[i],2)]) t_2=mod_32_addition([int(e_0(a),2),int(Maj(a,b,c),2)]) h=g g=f f=e e=mod_32_addition([int(d,2),t_1]) d=c c=b b=a a=mod_32_addition([t_1,t_2]) a=bin_32bit(a) e=bin_32bit(e) hash_0 = mod_32_addition([dec_return_hex(initial_hash_values[0]),int(a,2)]) hash_1 = mod_32_addition([dec_return_hex(initial_hash_values[1]),int(b,2)]) hash_2 = mod_32_addition([dec_return_hex(initial_hash_values[2]),int(c,2)]) hash_3 = mod_32_addition([dec_return_hex(initial_hash_values[3]),int(d,2)]) hash_4 = mod_32_addition([dec_return_hex(initial_hash_values[4]),int(e,2)]) hash_5 = mod_32_addition([dec_return_hex(initial_hash_values[5]),int(f,2)]) hash_6 = mod_32_addition([dec_return_hex(initial_hash_values[6]),int(g,2)]) hash_7 = mod_32_addition([dec_return_hex(initial_hash_values[7]),int(h,2)]) final_hash = (hex_return(hash_0), hex_return(hash_1), hex_return(hash_2), hex_return(hash_3), hex_return(hash_4), hex_return(hash_5), hex_return(hash_6), hex_return(hash_7)) return(final_hash) ```
Translating <http://en.wikipedia.org/wiki/SHA-2#SHA-256_.28a_SHA-2_variant.29_pseudocode> to Python should be straight forward.
7,321,698
I have used DATEDIFF function but since it requires yyyy/mm/dd this code is giving wrong value ,because i have stored date from date time picker in this formate day,month,dd,yyyy ``` private void button3_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); DBHandling db = new DBHandling(); db.GetDataset(" select JoiningDate from Emp where name = '" + textBox2.Text + "'", ref ds); db.GetDataset(" select LeavingDate from AddDate where name = '" + textBox2.Text + "'", ref ds1); string date1, date2; date1 = ds.Tables[0].Rows[0][0].ToString(); date2 = ds1.Tables[0].Rows[0][0].ToString(); db.GetDataset("select DATEDIFF(day,2008/1/15,2008/1/12) ", ref ds2); textBox5.Text = ds2.Tables[0].Rows[0][0].ToString(); } ``` so how should i do this so that i will gate right Difference between 2 dates i.e number of days between 2 dates,thanks in advance
2011/09/06
[ "https://Stackoverflow.com/questions/7321698", "https://Stackoverflow.com", "https://Stackoverflow.com/users/895965/" ]
``` initial_hash_values=[ '6a09e667','bb67ae85','3c6ef372','a54ff53a', '510e527f','9b05688c','1f83d9ab','5be0cd19' ] sha_256_constants=[ '428a2f98','71374491','b5c0fbcf','e9b5dba5', '3956c25b','59f111f1','923f82a4','ab1c5ed5', 'd807aa98','12835b01','243185be','550c7dc3', '72be5d74','80deb1fe','9bdc06a7','c19bf174', 'e49b69c1','efbe4786','0fc19dc6','240ca1cc', '2de92c6f','4a7484aa','5cb0a9dc','76f988da', '983e5152','a831c66d','b00327c8','bf597fc7', 'c6e00bf3','d5a79147','06ca6351','14292967', '27b70a85','2e1b2138','4d2c6dfc','53380d13', '650a7354','766a0abb','81c2c92e','92722c85', 'a2bfe8a1','a81a664b','c24b8b70','c76c51a3', 'd192e819','d6990624','f40e3585','106aa070', '19a4c116','1e376c08','2748774c','34b0bcb5', '391c0cb3','4ed8aa4a','5b9cca4f','682e6ff3', '748f82ee','78a5636f','84c87814','8cc70208', '90befffa','a4506ceb','bef9a3f7','c67178f2' ] def bin_return(dec): return(str(format(dec,'b'))) def bin_8bit(dec): return(str(format(dec,'08b'))) def bin_32bit(dec): return(str(format(dec,'032b'))) def bin_64bit(dec): return(str(format(dec,'064b'))) def hex_return(dec): return(str(format(dec,'x'))) def dec_return_bin(bin_string): return(int(bin_string,2)) def dec_return_hex(hex_string): return(int(hex_string,16)) def L_P(SET,n): to_return=[] j=0 k=n while k<len(SET)+1: to_return.append(SET[j:k]) j=k k+=n return(to_return) def s_l(bit_string): bit_list=[] for i in range(len(bit_string)): bit_list.append(bit_string[i]) return(bit_list) def l_s(bit_list): bit_string='' for i in range(len(bit_list)): bit_string+=bit_list[i] return(bit_string) def rotate_right(bit_string,n): bit_list = s_l(bit_string) count=0 while count <= n-1: list_main=list(bit_list) var_0=list_main.pop(-1) list_main=list([var_0]+list_main) bit_list=list(list_main) count+=1 return(l_s(list_main)) def shift_right(bit_string,n): bit_list=s_l(bit_string) count=0 while count <= n-1: bit_list.pop(-1) count+=1 front_append=['0']*n return(l_s(front_append+bit_list)) def mod_32_addition(input_set): value=0 for i in range(len(input_set)): value+=input_set[i] mod_32 = 4294967296 return(value%mod_32) def xor_2str(bit_string_1,bit_string_2): xor_list=[] for i in range(len(bit_string_1)): if bit_string_1[i]=='0' and bit_string_2[i]=='0': xor_list.append('0') if bit_string_1[i]=='1' and bit_string_2[i]=='1': xor_list.append('0') if bit_string_1[i]=='0' and bit_string_2[i]=='1': xor_list.append('1') if bit_string_1[i]=='1' and bit_string_2[i]=='0': xor_list.append('1') return(l_s(xor_list)) def and_2str(bit_string_1,bit_string_2): and_list=[] for i in range(len(bit_string_1)): if bit_string_1[i]=='1' and bit_string_2[i]=='1': and_list.append('1') else: and_list.append('0') return(l_s(and_list)) def or_2str(bit_string_1,bit_string_2): or_list=[] for i in range(len(bit_string_1)): if bit_string_1[i]=='0' and bit_string_2[i]=='0': or_list.append('0') else: or_list.append('1') return(l_s(or_list)) def not_str(bit_string): not_list=[] for i in range(len(bit_string)): if bit_string[i]=='0': not_list.append('1') else: not_list.append('0') return(l_s(not_list)) ''' SHA-256 Specific Functions: ''' def Ch(x,y,z): return(xor_2str(and_2str(x,y),and_2str(not_str(x),z))) def Maj(x,y,z): return(xor_2str(xor_2str(and_2str(x,y),and_2str(x,z)),and_2str(y,z))) def e_0(x): return(xor_2str(xor_2str(rotate_right(x,2),rotate_right(x,13)),rotate_right(x,22))) def e_1(x): return(xor_2str(xor_2str(rotate_right(x,6),rotate_right(x,11)),rotate_right(x,25))) def s_0(x): return(xor_2str(xor_2str(rotate_right(x,7),rotate_right(x,18)),shift_right(x,3))) def s_1(x): return(xor_2str(xor_2str(rotate_right(x,17),rotate_right(x,19)),shift_right(x,10))) def message_pad(bit_list): pad_one = bit_list + '1' pad_len = len(pad_one) k=0 while ((pad_len+k)-448)%512 != 0: k+=1 back_append_0 = '0'*k back_append_1 = bin_64bit(len(bit_list)) return(pad_one+back_append_0+back_append_1) def message_bit_return(string_input): bit_list=[] for i in range(len(string_input)): bit_list.append(bin_8bit(ord(string_input[i]))) return(l_s(bit_list)) def message_pre_pro(input_string): bit_main = message_bit_return(input_string) return(message_pad(bit_main)) def message_parsing(input_string): return(L_P(message_pre_pro(input_string),32)) def message_schedule(index,w_t): new_word = bin_32bit(mod_32_addition([int(s_1(w_t[index-2]),2),int(w_t[index-7],2),int(s_0(w_t[index-15]),2),int(w_t[index-16],2)])) return(new_word) ''' This example of SHA_256 works for an input string <56 characters. ''' def sha_256(input_string): assert len(input_string) < 56, "This example of SHA_256 works for an input string <56 characters." w_t=message_parsing(input_string) a=bin_32bit(dec_return_hex(initial_hash_values[0])) b=bin_32bit(dec_return_hex(initial_hash_values[1])) c=bin_32bit(dec_return_hex(initial_hash_values[2])) d=bin_32bit(dec_return_hex(initial_hash_values[3])) e=bin_32bit(dec_return_hex(initial_hash_values[4])) f=bin_32bit(dec_return_hex(initial_hash_values[5])) g=bin_32bit(dec_return_hex(initial_hash_values[6])) h=bin_32bit(dec_return_hex(initial_hash_values[7])) for i in range(0,64): if i <= 15: t_1=mod_32_addition([int(h,2),int(e_1(e),2),int(Ch(e,f,g),2),int(sha_256_constants[i],16),int(w_t[i],2)]) t_2=mod_32_addition([int(e_0(a),2),int(Maj(a,b,c),2)]) h=g g=f f=e e=mod_32_addition([int(d,2),t_1]) d=c c=b b=a a=mod_32_addition([t_1,t_2]) a=bin_32bit(a) e=bin_32bit(e) if i > 15: w_t.append(message_schedule(i,w_t)) t_1=mod_32_addition([int(h,2),int(e_1(e),2),int(Ch(e,f,g),2),int(sha_256_constants[i],16),int(w_t[i],2)]) t_2=mod_32_addition([int(e_0(a),2),int(Maj(a,b,c),2)]) h=g g=f f=e e=mod_32_addition([int(d,2),t_1]) d=c c=b b=a a=mod_32_addition([t_1,t_2]) a=bin_32bit(a) e=bin_32bit(e) hash_0 = mod_32_addition([dec_return_hex(initial_hash_values[0]),int(a,2)]) hash_1 = mod_32_addition([dec_return_hex(initial_hash_values[1]),int(b,2)]) hash_2 = mod_32_addition([dec_return_hex(initial_hash_values[2]),int(c,2)]) hash_3 = mod_32_addition([dec_return_hex(initial_hash_values[3]),int(d,2)]) hash_4 = mod_32_addition([dec_return_hex(initial_hash_values[4]),int(e,2)]) hash_5 = mod_32_addition([dec_return_hex(initial_hash_values[5]),int(f,2)]) hash_6 = mod_32_addition([dec_return_hex(initial_hash_values[6]),int(g,2)]) hash_7 = mod_32_addition([dec_return_hex(initial_hash_values[7]),int(h,2)]) final_hash = (hex_return(hash_0), hex_return(hash_1), hex_return(hash_2), hex_return(hash_3), hex_return(hash_4), hex_return(hash_5), hex_return(hash_6), hex_return(hash_7)) return(final_hash) ```
Some time ago I was also studying SHA-256 and created pure-python class that implements this hash. If I remember correctly, mostly I've taken algorithm from Wikipedia [SHA-256 Pseudocode](https://en.wikipedia.org/wiki/SHA-2#Pseudocode) and partially from some open-source projects. Algorithm doesn't import any (even standard) modules. Of cause it is much slower than `hashlib`'s variant and only meant for studying. If you just run the script it executes 1000 tests comparing `hashlib`'s and my variants. Only testing function imports some modules, algorithm's class itself doesn't need any modules. Interface is same as in hashlib's sha256 class. See `test()` function for examples of usage. [Try it online!](https://tio.run/##lVZNc9s2EL37V6CHVmRtKwAIgKQnyfTk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x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ] hs = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ] M32 = 0xFFFFFFFF def __init__(self, m = None): self.mlen = 0 self.buf = b'' self.k = self.ks[:] self.h = self.hs[:] self.fin = False if m is not None: self.update(m) @staticmethod def pad(mlen): mdi = mlen & 0x3F length = (mlen << 3).to_bytes(8, 'big') padlen = 55 - mdi if mdi < 56 else 119 - mdi return b'\x80' + b'\x00' * padlen + length @staticmethod def ror(x, y): return ((x >> y) | (x << (32 - y))) & Sha256.M32 @staticmethod def maj(x, y, z): return (x & y) ^ (x & z) ^ (y & z) @staticmethod def ch(x, y, z): return (x & y) ^ ((~x) & z) def compress(self, c): w = [0] * 64 w[0 : 16] = [int.from_bytes(c[i : i + 4], 'big') for i in range(0, len(c), 4)] for i in range(16, 64): s0 = self.ror(w[i - 15], 7) ^ self.ror(w[i - 15], 18) ^ (w[i - 15] >> 3) s1 = self.ror(w[i - 2], 17) ^ self.ror(w[i - 2], 19) ^ (w[i - 2] >> 10) w[i] = (w[i - 16] + s0 + w[i - 7] + s1) & self.M32 a, b, c, d, e, f, g, h = self.h for i in range(64): s0 = self.ror(a, 2) ^ self.ror(a, 13) ^ self.ror(a, 22) t2 = s0 + self.maj(a, b, c) s1 = self.ror(e, 6) ^ self.ror(e, 11) ^ self.ror(e, 25) t1 = h + s1 + self.ch(e, f, g) + self.k[i] + w[i] h = g g = f f = e e = (d + t1) & self.M32 d = c c = b b = a a = (t1 + t2) & self.M32 for i, (x, y) in enumerate(zip(self.h, [a, b, c, d, e, f, g, h])): self.h[i] = (x + y) & self.M32 def update(self, m): if m is None or len(m) == 0: return assert not self.fin, 'Hash already finalized and can not be updated!' self.mlen += len(m) m = self.buf + m for i in range(0, len(m) // 64): self.compress(m[64 * i : 64 * (i + 1)]) self.buf = m[len(m) - (len(m) % 64):] def digest(self): if not self.fin: self.update(self.pad(self.mlen)) self.digest = b''.join(x.to_bytes(4, 'big') for x in self.h[:8]) self.fin = True return self.digest def hexdigest(self): tab = '0123456789abcdef' return ''.join(tab[b >> 4] + tab[b & 0xF] for b in self.digest()) def test(): import secrets, hashlib, random for itest in range(500): data = secrets.token_bytes(random.randrange(257)) a, b = hashlib.sha256(data).hexdigest(), Sha256(data).hexdigest() assert a == b, (a, b) for itest in range(500): a, b = hashlib.sha256(), Sha256() for j in range(random.randrange(10)): data = secrets.token_bytes(random.randrange(129)) a.update(data) b.update(data) a, b = a.hexdigest(), b.hexdigest() assert a == b, (a, b) print('Sha256 tested successfully.') if __name__ == '__main__': test() ```
7,321,698
I have used DATEDIFF function but since it requires yyyy/mm/dd this code is giving wrong value ,because i have stored date from date time picker in this formate day,month,dd,yyyy ``` private void button3_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); DBHandling db = new DBHandling(); db.GetDataset(" select JoiningDate from Emp where name = '" + textBox2.Text + "'", ref ds); db.GetDataset(" select LeavingDate from AddDate where name = '" + textBox2.Text + "'", ref ds1); string date1, date2; date1 = ds.Tables[0].Rows[0][0].ToString(); date2 = ds1.Tables[0].Rows[0][0].ToString(); db.GetDataset("select DATEDIFF(day,2008/1/15,2008/1/12) ", ref ds2); textBox5.Text = ds2.Tables[0].Rows[0][0].ToString(); } ``` so how should i do this so that i will gate right Difference between 2 dates i.e number of days between 2 dates,thanks in advance
2011/09/06
[ "https://Stackoverflow.com/questions/7321698", "https://Stackoverflow.com", "https://Stackoverflow.com/users/895965/" ]
``` initial_hash_values=[ '6a09e667','bb67ae85','3c6ef372','a54ff53a', '510e527f','9b05688c','1f83d9ab','5be0cd19' ] sha_256_constants=[ '428a2f98','71374491','b5c0fbcf','e9b5dba5', '3956c25b','59f111f1','923f82a4','ab1c5ed5', 'd807aa98','12835b01','243185be','550c7dc3', '72be5d74','80deb1fe','9bdc06a7','c19bf174', 'e49b69c1','efbe4786','0fc19dc6','240ca1cc', '2de92c6f','4a7484aa','5cb0a9dc','76f988da', '983e5152','a831c66d','b00327c8','bf597fc7', 'c6e00bf3','d5a79147','06ca6351','14292967', '27b70a85','2e1b2138','4d2c6dfc','53380d13', '650a7354','766a0abb','81c2c92e','92722c85', 'a2bfe8a1','a81a664b','c24b8b70','c76c51a3', 'd192e819','d6990624','f40e3585','106aa070', '19a4c116','1e376c08','2748774c','34b0bcb5', '391c0cb3','4ed8aa4a','5b9cca4f','682e6ff3', '748f82ee','78a5636f','84c87814','8cc70208', '90befffa','a4506ceb','bef9a3f7','c67178f2' ] def bin_return(dec): return(str(format(dec,'b'))) def bin_8bit(dec): return(str(format(dec,'08b'))) def bin_32bit(dec): return(str(format(dec,'032b'))) def bin_64bit(dec): return(str(format(dec,'064b'))) def hex_return(dec): return(str(format(dec,'x'))) def dec_return_bin(bin_string): return(int(bin_string,2)) def dec_return_hex(hex_string): return(int(hex_string,16)) def L_P(SET,n): to_return=[] j=0 k=n while k<len(SET)+1: to_return.append(SET[j:k]) j=k k+=n return(to_return) def s_l(bit_string): bit_list=[] for i in range(len(bit_string)): bit_list.append(bit_string[i]) return(bit_list) def l_s(bit_list): bit_string='' for i in range(len(bit_list)): bit_string+=bit_list[i] return(bit_string) def rotate_right(bit_string,n): bit_list = s_l(bit_string) count=0 while count <= n-1: list_main=list(bit_list) var_0=list_main.pop(-1) list_main=list([var_0]+list_main) bit_list=list(list_main) count+=1 return(l_s(list_main)) def shift_right(bit_string,n): bit_list=s_l(bit_string) count=0 while count <= n-1: bit_list.pop(-1) count+=1 front_append=['0']*n return(l_s(front_append+bit_list)) def mod_32_addition(input_set): value=0 for i in range(len(input_set)): value+=input_set[i] mod_32 = 4294967296 return(value%mod_32) def xor_2str(bit_string_1,bit_string_2): xor_list=[] for i in range(len(bit_string_1)): if bit_string_1[i]=='0' and bit_string_2[i]=='0': xor_list.append('0') if bit_string_1[i]=='1' and bit_string_2[i]=='1': xor_list.append('0') if bit_string_1[i]=='0' and bit_string_2[i]=='1': xor_list.append('1') if bit_string_1[i]=='1' and bit_string_2[i]=='0': xor_list.append('1') return(l_s(xor_list)) def and_2str(bit_string_1,bit_string_2): and_list=[] for i in range(len(bit_string_1)): if bit_string_1[i]=='1' and bit_string_2[i]=='1': and_list.append('1') else: and_list.append('0') return(l_s(and_list)) def or_2str(bit_string_1,bit_string_2): or_list=[] for i in range(len(bit_string_1)): if bit_string_1[i]=='0' and bit_string_2[i]=='0': or_list.append('0') else: or_list.append('1') return(l_s(or_list)) def not_str(bit_string): not_list=[] for i in range(len(bit_string)): if bit_string[i]=='0': not_list.append('1') else: not_list.append('0') return(l_s(not_list)) ''' SHA-256 Specific Functions: ''' def Ch(x,y,z): return(xor_2str(and_2str(x,y),and_2str(not_str(x),z))) def Maj(x,y,z): return(xor_2str(xor_2str(and_2str(x,y),and_2str(x,z)),and_2str(y,z))) def e_0(x): return(xor_2str(xor_2str(rotate_right(x,2),rotate_right(x,13)),rotate_right(x,22))) def e_1(x): return(xor_2str(xor_2str(rotate_right(x,6),rotate_right(x,11)),rotate_right(x,25))) def s_0(x): return(xor_2str(xor_2str(rotate_right(x,7),rotate_right(x,18)),shift_right(x,3))) def s_1(x): return(xor_2str(xor_2str(rotate_right(x,17),rotate_right(x,19)),shift_right(x,10))) def message_pad(bit_list): pad_one = bit_list + '1' pad_len = len(pad_one) k=0 while ((pad_len+k)-448)%512 != 0: k+=1 back_append_0 = '0'*k back_append_1 = bin_64bit(len(bit_list)) return(pad_one+back_append_0+back_append_1) def message_bit_return(string_input): bit_list=[] for i in range(len(string_input)): bit_list.append(bin_8bit(ord(string_input[i]))) return(l_s(bit_list)) def message_pre_pro(input_string): bit_main = message_bit_return(input_string) return(message_pad(bit_main)) def message_parsing(input_string): return(L_P(message_pre_pro(input_string),32)) def message_schedule(index,w_t): new_word = bin_32bit(mod_32_addition([int(s_1(w_t[index-2]),2),int(w_t[index-7],2),int(s_0(w_t[index-15]),2),int(w_t[index-16],2)])) return(new_word) ''' This example of SHA_256 works for an input string <56 characters. ''' def sha_256(input_string): assert len(input_string) < 56, "This example of SHA_256 works for an input string <56 characters." w_t=message_parsing(input_string) a=bin_32bit(dec_return_hex(initial_hash_values[0])) b=bin_32bit(dec_return_hex(initial_hash_values[1])) c=bin_32bit(dec_return_hex(initial_hash_values[2])) d=bin_32bit(dec_return_hex(initial_hash_values[3])) e=bin_32bit(dec_return_hex(initial_hash_values[4])) f=bin_32bit(dec_return_hex(initial_hash_values[5])) g=bin_32bit(dec_return_hex(initial_hash_values[6])) h=bin_32bit(dec_return_hex(initial_hash_values[7])) for i in range(0,64): if i <= 15: t_1=mod_32_addition([int(h,2),int(e_1(e),2),int(Ch(e,f,g),2),int(sha_256_constants[i],16),int(w_t[i],2)]) t_2=mod_32_addition([int(e_0(a),2),int(Maj(a,b,c),2)]) h=g g=f f=e e=mod_32_addition([int(d,2),t_1]) d=c c=b b=a a=mod_32_addition([t_1,t_2]) a=bin_32bit(a) e=bin_32bit(e) if i > 15: w_t.append(message_schedule(i,w_t)) t_1=mod_32_addition([int(h,2),int(e_1(e),2),int(Ch(e,f,g),2),int(sha_256_constants[i],16),int(w_t[i],2)]) t_2=mod_32_addition([int(e_0(a),2),int(Maj(a,b,c),2)]) h=g g=f f=e e=mod_32_addition([int(d,2),t_1]) d=c c=b b=a a=mod_32_addition([t_1,t_2]) a=bin_32bit(a) e=bin_32bit(e) hash_0 = mod_32_addition([dec_return_hex(initial_hash_values[0]),int(a,2)]) hash_1 = mod_32_addition([dec_return_hex(initial_hash_values[1]),int(b,2)]) hash_2 = mod_32_addition([dec_return_hex(initial_hash_values[2]),int(c,2)]) hash_3 = mod_32_addition([dec_return_hex(initial_hash_values[3]),int(d,2)]) hash_4 = mod_32_addition([dec_return_hex(initial_hash_values[4]),int(e,2)]) hash_5 = mod_32_addition([dec_return_hex(initial_hash_values[5]),int(f,2)]) hash_6 = mod_32_addition([dec_return_hex(initial_hash_values[6]),int(g,2)]) hash_7 = mod_32_addition([dec_return_hex(initial_hash_values[7]),int(h,2)]) final_hash = (hex_return(hash_0), hex_return(hash_1), hex_return(hash_2), hex_return(hash_3), hex_return(hash_4), hex_return(hash_5), hex_return(hash_6), hex_return(hash_7)) return(final_hash) ```
If you only want the hash value: ``` from hashlib import sha256 data = input('Enter plaintext data: ') output = sha256(data.encode('utf-8')) print(output) ``` Python's hashlib also has SHA-1, SHA-384, SHA-512, and MD5 hash functions.